Skip to content

Instantly share code, notes, and snippets.

@inoyakaigor
Created November 30, 2018 15:50
Show Gist options
  • Save inoyakaigor/c1b01030e8a5c18513edd9c8ab16bbe6 to your computer and use it in GitHub Desktop.
Save inoyakaigor/c1b01030e8a5c18513edd9c8ab16bbe6 to your computer and use it in GitHub Desktop.
Пример синхронизации двух полей с помощью React Context
import React from "react";
import ReactDOM from "react-dom";
const UserContext = React.createContext();
const Donor = () => (
<UserContext.Consumer>
{
store => {
const { value, onChange } = store
return <input onChange={onChange} value={value} />
}
}
</UserContext.Consumer>
);
const Recipient = () => (
<UserContext.Consumer>
{
store => {
const { value, onChange } = store
return <input onChange={onChange} value={value} />
}
}
</UserContext.Consumer>
)
class App extends React.Component {
constructor() {
super()
this.state = {
value: ''
}
}
onChange = event => this.setState({value: event.currentTarget.value})
render() {
return (
<UserContext.Provider value={{value: this.state.value, onChange: this.onChange}}>
<div className="App">
<h1>Hello CodeSandbox</h1>
<Donor />
<Recipient />
</div>
</UserContext.Provider>
)
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment