Last active
November 19, 2018 08:36
-
-
Save anishtr4/5531c6cc71af7eca40fc7c0d8b9f8222 to your computer and use it in GitHub Desktop.
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, { Component } from "react"; | |
import "./App.css"; | |
import InputContext from "./context"; | |
function ParentComponent() { | |
return ( | |
<ChildComponent /> | |
); | |
} | |
function ChildComponent() { | |
return ( | |
<GrandChildComponent /> | |
); | |
} | |
function GrandChildComponent(props) { | |
return ( | |
<div><InputContext.Consumer>{val => <p>{val}</p>}</InputContext.Consumer></div> | |
); | |
} | |
class ContextValue extends Component { | |
state = { | |
inputdata: 0 | |
}; | |
updateInputValue = (evt) => { | |
this.setState({ | |
inputdata: evt.target.value | |
}); | |
} | |
render() { | |
return ( | |
<div > | |
<InputContext.Provider value={this.state.inputdata}> | |
<ParentComponent /> | |
</InputContext.Provider> | |
<input value={this.state.inputdata} onChange={this.updateInputValue} /> | |
</div> | |
); | |
} | |
} | |
export default ContextValue; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment