Created
August 9, 2018 20:41
-
-
Save mharris717/65296c385ef6b2d3fdf6cb08aceaa141 to your computer and use it in GitHub Desktop.
React Microstates Waaaaat
This file contains 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
import React from "react"; | |
import { Consumer } from "@microstates/react"; | |
import { map } from "microstates"; | |
export default class Blog extends React.Component { | |
render() { | |
return ( | |
<Consumer> | |
{blog => { | |
return ( | |
<div> | |
<h1>{blog.title.state}</h1> | |
<hr /> | |
{map(blog.posts, post => ( | |
<Post getMS={() => post} key={post.state.title} /> | |
))} | |
</div> | |
); | |
}} | |
</Consumer> | |
); | |
} | |
} | |
export class Post extends React.Component { | |
constructor(props) { | |
super(props); | |
this.commentBox = React.createRef(); | |
} | |
addComment(body) { | |
this.props.getMS().comments.push({ body }); | |
} | |
render() { | |
let { body, title, author, comments } = this.props.getMS().state; | |
return ( | |
<div> | |
<h2>{title}</h2> | |
<span>by {author.fullName}</span> | |
<br /> <br /> | |
<div>{body}</div> | |
<span>{comments.length} Comments</span> | |
<br /> <br /> | |
<h4>Add Comment</h4> | |
<textarea ref={this.commentBox} /> | |
<button onClick={() => this.addComment(this.commentBox.current.value)}> | |
Comment | |
</button> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment