Skip to content

Instantly share code, notes, and snippets.

@anishtr4
Last active November 19, 2018 08:36
Show Gist options
  • Save anishtr4/5531c6cc71af7eca40fc7c0d8b9f8222 to your computer and use it in GitHub Desktop.
Save anishtr4/5531c6cc71af7eca40fc7c0d8b9f8222 to your computer and use it in GitHub Desktop.
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