Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ask-42 authored Jun 12, 2019
1 parent 66be414 commit 20c31c0
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,52 @@
- Prevents submission of input until all fields are filled in
- Login/Signup modals so users can authenticate from anywhere without leaving the current page, and enables additional app functionality on signup via mapping to redux state

## Code

The standard practice to conditionally render content `{condition && content}` or `{ condition ? content : alternateContent }` begins to become unreadable quickly in complicated components - sometimes a smell that a component could be extracted, but even for smaller things, ugly and difficult to maintain.

My solution, the elegant `<If />` component.

```JS
export default (props) => (
(props.true || props.When) ?
(typeof props.Then === "function" ? props.Then() : props.Then || "") :
(typeof props.Else === "function" ? props.Else() : props.Else || "")
);
```

`If` takes in three props:
1. A boolean `When` value (the word `When` is optional but recommended)
2. `Then` - optional content to render if `When` is true, or a callback function to execute and render; defaults to `""`.
3. `Else` - optional content to render if `When` is false, or a callback function to execute and render; defaults to `""`.

This let me rewrite my code to look more akin to the standard `if (condition) {...} else {...}`

```JS
latestVideos(){
return(
<>
<h2>Latest Videos</h2>
<ul className="video-index latest-videos">
<If
When={this.props.latestVideos.length === 0}
Then={<li className="no-videos">No Videos</li>}
Else={this.props.latestVideos.map(video=><li key={video.id}><VideoIndexItem video={video} /></li>)}
/>
</ul>
</>
)
}
```

or, omitting the optional `When`, this can become

```JS
<If {this.props.latestVideos.length === 0}
Then={<li className="no-videos">No Videos</li>}
Else={this.props.latestVideos.map(video=><li key={video.id}><VideoIndexItem video={video} /></li>)}
/>
```

## Possible Future Plans

Expand Down

0 comments on commit 20c31c0

Please sign in to comment.