React Container Scope Is Not Inherited By Component
I've seperated container and component and now facing the issues with rerendering component.
render() {
return <DomainEdit schema={this.props.schema} domain={this.domain} formData={this.props.formData} updateSubType={this.updateSubType} types={this.props.types}/>;
}
I am sending updateSubType
method which is a callback method whenever anything in the form is changed.
After its changed I am sending back what is changed and I have to update this.props.schema in order to re-render the component with the new schema.
The problem I am facing is that the callback methods this
is the scope of the component and not container itself, in other words component only has the arguments I've sent in the render method, which is a problem because I can't access the container props and re-render the component.
How could I re-render the component in the callback method which is defined in the container ?
Answer
It looks like a context binding issue.
If you want updateSubType to be called inside DomainEdit and still have this
inside this callback pointing to the container reference, you have to bind updateSubType to the container instance.
Two ways are available to do this:
In the constructor of the container, redefine this.updateSubType = this.updateSubType.bind(this);
Or if you're using ES6, define updateSubType this way:
updateSubType = () => {
...method definition
}
The arrow function notation (() => {}
) auto binds the function to the surrounding instance.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM