Created
November 30, 2018 15:50
-
-
Save inoyakaigor/c1b01030e8a5c18513edd9c8ab16bbe6 to your computer and use it in GitHub Desktop.
Пример синхронизации двух полей с помощью React Context
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 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