Last active
March 1, 2018 22:34
-
-
Save nahumzs/fbea7bcf7ea2235323a09075a14caa33 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export default class Form extends Component { | |
// Defining Context Types these are the properties that | |
// the consumer component can use. | |
static childContextTypes = { | |
onSent: func, | |
isDisabled: bool, | |
} | |
// Providing the actual values for the Context Types | |
// defined in childContextTypes | |
getChildContext() { | |
const { isDisabled } = this.props; | |
return { | |
isDisabled: this.state.isDisabled, | |
onSent: this.handleOnSent, | |
} | |
} | |
state = { | |
isDisabled: false, | |
} | |
handleOnSent = event => { | |
// having the callback in the parent component | |
// would allowed you theoretically update the state | |
// or process any business logic that you need. | |
event.preventDefault(); | |
this.setState({ isDisabled: true }); | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
this.setState({ isDisabled: false }); | |
resolve(true); | |
}, 3000); | |
}); | |
} | |
render() { | |
return ( | |
<form className="children-extend-form"> | |
{this.props.children} | |
</form> | |
); | |
} | |
} |
This file contains hidden or 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
// Statless component can received the context via the second argument | |
export const Input = (props, context) => { | |
return <input type="text" disabled={context.isDisabled} /> | |
} | |
// Stateless component also can access the context | |
// properties as well adding contextTypes | |
Input.contextTypes = { | |
isDisabled: bool, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment