Skip to content

Instantly share code, notes, and snippets.

@ZanshinPost
Forked from markerikson/render-logic.js
Created December 3, 2017 16:08
Show Gist options
  • Save ZanshinPost/a01508f221b00581ccc5c326ad93f8f3 to your computer and use it in GitHub Desktop.
Save ZanshinPost/a01508f221b00581ccc5c326ad93f8f3 to your computer and use it in GitHub Desktop.
React render function organization
class ParentComponent extends Component {
render() {
// My basic render function structure:
// 1) Extract values from props and state
const {a, b, someBoolean, someList} = this.props;
// 2) Render any dependent items into temporary variables, such as conditional components or lists
const conditionalComponent = someBoolean ? <SomeComponent /> : null;
const listItems = someList.map(item => <ListItem item={item} />);
// 3) Use a single JSX structure, with the temporary variables in the correct spots.
// Note that I avoid logic inline in my JSX. That's perfectly legal, but I prefer to
// make the use of that logic as clear as possible by keeping it outside the JSX.
return (
<div>
<div>A: {a}, B: {b}</div>
{conditionalComponent}
{listItems}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment