Created
November 6, 2017 13:59
-
-
Save Siemko/c0998da19d65793aef9b131d7ecd5482 to your computer and use it in GitHub Desktop.
React components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MyBetterDiv = props => ( | |
<div className="better" {...props} /> | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="better" data-gorrion="gorrion" data-other="other prop">Gorrion is always bettter</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<MyBetterDiv data-gorrion="gorrion" data-other="other prop">Gorrion is always bettter</MyBetterDiv> |
Raw
commentList.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
const Commentlist = ({comments}) => ( | |
<ul> | |
{comments.map(({ body, author }) => | |
<li>{body}-{author}</li> | |
)} | |
</ul> | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommentListContainer extends React.Component { | |
constructor() { | |
super(); | |
this.state = { comments: [] } | |
} | |
componentDidMount() { | |
getAsync("https://imaginary-api.gorrion.io/my-comments") | |
.then(comments => this.setState({ comments })) | |
} | |
render() { | |
return <CommentList comments={this.state.comments} />; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Connect = ComposedComponent => | |
class extends React.Component { | |
constructor() { | |
super() | |
this.state = { name: "" } | |
} | |
componentDidMount() { | |
//fetching data is a part of HOC | |
getAsync('https://imaginary-api.gorrion.io/name') | |
.then(({ name }) => { this.setState({ name: name }) }) | |
} | |
render() { | |
return ( | |
<ComposedComponent | |
{...this.props} | |
name={this.state.name} | |
/> | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Greeting = props => <div>Hello {props.name}!</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Greeting = ({ name, ...props }) => ( | |
<div {...props}>Hi {name}!</div> | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Greeting = ({name}) => <div>Hello {name}!</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Greeting = ({ name, ...props }) => { | |
if (!name) { | |
return <div>Loading...</div> | |
} | |
return <div {...props}>Hi {name}!</div> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Loading = () => <p>Loading...</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Worker = props => <p>{props.message}</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WorkerInfo extends React.Component { | |
state = { worker: null } | |
componentDidMount() { | |
// We can make an ajax call here, for e.g. | |
setTimeout(() => this.setState({ | |
worker: `Hi, I am ${this.props.name} and I am proud to work for ${this.props.employer}` | |
}), 2500); | |
} | |
render() { | |
// Render the children with a function using state as the argument | |
return this.props.children(this.state.worker); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment