Skip to content

Instantly share code, notes, and snippets.

@deniztetik
Created July 8, 2018 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deniztetik/d73087c9b830e5d58839a1d8bd4b0aef to your computer and use it in GitHub Desktop.
Save deniztetik/d73087c9b830e5d58839a1d8bd4b0aef to your computer and use it in GitHub Desktop.
// ./src/App.js
import React, { Component } from 'react';
import './App.css';
import Post from './Post';
import EditForm from './EditForm';
import { AppContext, context } from './app-context';
class App extends Component {
/* .addUpVote() and .savePost() are added to the state object which is
then added to the Context Provider's value prop */
addUpVote = () => {
this.setState({
upvotes: this.state.upvotes + 1,
});
}
savePost = (post) => {
const {
title,
content,
} = post;
this.setState({
title,
content,
})
}
state = {
title: context.title,
content: context.content,
savePost: this.savePost,
upvotes: context.upvotes,
addUpVote: this.addUpVote,
}
render() {
return (
<AppContext.Provider value={this.state}>
<div className="main">
<Post />
<EditForm />
</div>
</AppContext.Provider>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment