javascript - React JS - trigger event on certain state value - Stack Overflow
How can I trigger an event from a parent ponent when its child ponent's state holds a certain value?
For example: say I have a Parent ponent and a Child ponent, where the parent's state has the property activeIndex and the child's state has the property activeTask.
I want to trigger the parent's props.handleNextChild method if the child's state's activeTask property equals 3.
How would I do that?
How can I trigger an event from a parent ponent when its child ponent's state holds a certain value?
For example: say I have a Parent ponent and a Child ponent, where the parent's state has the property activeIndex and the child's state has the property activeTask.
I want to trigger the parent's props.handleNextChild method if the child's state's activeTask property equals 3.
How would I do that?
Share Improve this question edited Apr 26, 2017 at 4:56 Chris Martin 30.8k12 gold badges80 silver badges141 bronze badges asked Apr 26, 2017 at 3:59 nuT707nuT707 1,5734 gold badges17 silver badges28 bronze badges3 Answers
Reset to default 3Whenever the state or props of your ponent change, the lifecycle method ponentDidUpdate
gets called after the update is done, so you could pass handleNextChild
as a prop to your child:
class Parent extends Component {
handleNextChild() {
//do whatever
}
render() {
return (
//stuff
<Child callback={() => handleNextChild()} /> //arrow for lexical bind
//more stuff
);
}
}
and change your child ponent class:
class Child extends Component {
constructor(props) {
super(props);
this.callbackHandled = false
}
// ...child class code...
ponentDidUpdate() {
if (this.state.activeTask === 3) {
this.callbackHandled = false;
this.setState({ activeTask: resetValue }, () => {
if (!this.callbackHandled) {
this.callbackHandled = true;
this.props.callback();
}
});
}
render() {
// render your child ponent...
}
}
This will get triggered whenever your state's activeTask
property changes to 3. However, as long as activeTask
stays at 3, it will run on every update, so if you want it to run only once, be sure to setState
to reset activeTask
before you call callback
as I have done. I have used the callback version of setState
and an nstance variable in order to ensure the state gets set before the callback (aka: handleNextChild
) is called in order to avoid calling it multiple times, and to ensure that the child gets handled exactly once each time activeTask
is set to 3.
There is no straight way to achieve this, create a call back in parent and pass it down to child via props.Then keep checking in the child if the desired condition is met and then trigger the callback.
pseudo code:
Parent
class Parent extends Component {
handleNextChild = () => {
// parent logic es here
}
render() {
return (
<Child handleNextChild = this.handleNextChild />
);
}
}
Child:
class Child extends Component {
onChange = () => {
if(this.state.activeTask === 3){
this.props.handleNextChild();
}
}.....
So I've changed the logic of my app: just set active child index in my json-data and use it as state
@Statements = React.createClass
displayName: 'Statements'
getInitialState: ->
statements: @props.statements
attempt: @props.attempt
activeIndex: @props.attempt.active_statement
updateStates: (data) ->
@setState activeIndex: data.attempt.active_statement
@setState attempt: data.attempt
@setState statements: data.statements
- 谷歌:无意统一Android和Chrome OS
- selenium webdriver - Instagram "Post" button when sending Python comment - Stack Overflow
- android - Room Database is very slow For query 20 results with limit in 25k records - Stack Overflow
- for loop - CSS masonry grid with dynamic rows width - Stack Overflow
- html - Disable scrolling. Overflow:hidden not working - Stack Overflow
- google bigquery - How to Load Large Tables Beyond the 10GB Query Limit in Power BI Premium? - Stack Overflow
- dart - Flutter - dyld: lazy symbol binding failed: Symbol not found: _os_log_create - Stack Overflow
- python - Assist LSP with dynamically imported methods - Stack Overflow
- python - CERT_PATH points to the wrong directory even though I set it correctly in my Django project - Stack Overflow
- node.js - Cookies Blocked in Cross-Origin Requests Between Netlify Frontend(Reactjs) and Railway Backend (Node ts) - Stack Overf
- c++ - How to create a class setup where two classes each holds an instance of the other? - Stack Overflow
- node.js - How to handle time taking processes in slack app view - Stack Overflow
- html - django multi-level template extending - Stack Overflow
- angular - How to resolve vitest errors; stylesheet imports and dynamically fetched modules - Stack Overflow
- c# - Instantiated gameObject in Unity will not update the transform.position - Stack Overflow
- Swift Predicate: Fatal Error Keypaths With Multiple Components - Stack Overflow
- oop - How can I link an object to another object in Java? - Stack Overflow