score:1
you can dipatch the api call this.props.fetchdata(...)
in the parent. then update this.state.activecolor
in parent. now pass that value as props to child1
or child2
.
inside your child components, since you're dispatching the action in componentwillmount
, there won't happen a new api call in them when you supply new props from parent.
if you want to update internal states (if any) of the child components upon receiving the new props, you may use componentwillreceiveprops(nextprops)
.
update:
child1
and child2
receives new color from parent via props.
so if you want to trigger the api call on receiving new props, dispatch this.props.fetchdata(...)
inside componentwillreceiveprops
of child components.
hope you are using es6 classes:
componentwillreceiveprops(nextprops) {
if (this.props.colorvalue !== nextprops.colorvalue) {
this.props.fetchdata(this.props.somename, nextprops.colorvalue);
}
}
also i am assuming you have mapped the action creator fetchdata
as props to your child components using connect
.
Source: stackoverflow.com
Related Query
- calling action creators on state/props change
- Updating state on props change in React Form
- How to change props to state in React Hooks?
- React shouldComponentUpdate() is called even when props or state for that component did not change
- Can we pass setState as props from one component to other and change parent state from child component in React?
- Calling functions after state change occurs in reactjs
- Props not updating when redux state change in React Hooks
- Which of these strategies is the best way to reset a component's state when the props change
- In React componentDidUpdate, can props and state change at the same time?
- getDerivedStateFromProps, change of state under the influence of changing props
- Take an action on props change in ReactJS
- Change state of nested object props based on user input handleChange
- How to wait for a Redux action to change state from a React component
- Update state values with props change in reactjs
- How to mock props that actually are connected action creators of Redux?
- action creators not available as functions in props when passed directly to connect
- Calling a method after state change
- React - change state when props are changed
- React update child's props after parent's state change
- State use as props in component, state change will not re-render the component
- How to change button state and dispatch action to redux?
- Action creators handling axios get.request with state access for param
- How to correctly type action creators mapped to props in react-redux
- Action is not dispatched again after the props change in React-Redux
- How to reset state on props change in an already mounted component?
- React is not re-rendering on state and props change
- Type error when calling mapped action - mapping actions to props react/redux
- Calling a function to change state in parent component from the child component in react
- Does change in Props Data Change the state of components or will it render the change?
- Set state from props change in react functional component?
More Query from same tag
- How to change children on parent event?
- how to change toastify loading bg while updating
- React call a function from other component
- How do I style react-table filter input field?
- Multiple API Calls in React
- Toggle Drawer from AppBar LeftIcon
- row action based on row data in react material-table
- setTimeout() function called before duration of timeout?
- Semantic-ui-react: Custom Search issue with React/TypeScript and Redux
- Really need some help figuring out the logic of componentWillMount() prior to render
- React-router can't redirect to "/" route
- how to not duplicate react i18next in each component
- the marker is added after the second click state hasn't been updated with the newItem you added yet?
- click event refresh my page
- React presentational component unable to read value for <input /> from redux store using react container
- How can I implement a condition in the renderCell method of a MUI GridColDef component?
- Migrate to new component on clicking submit button in react
- Inline styles for tables in ReactJS/JSX
- Having a component re-render on button press in React
- this.props react training Question - CodeAcademy
- Nested routing is not working in React Router dom V4 from individual component and works only from App.js component
- Creating a class in react and exporting with higher order components
- "Failed to Compile" "Parsing error: Unexpected token :" during attempt to migrate to typescript in existing create-react-app
- How to separate user's name and the text inside a chat box?
- How to use react-spring from react component classes
- Express JS conditional routing -How to route Multiple payment gateways?
- React - Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- How to connect state to props with mobx.js @observer when use ES6 class?
- how to create React Context for a function value without re-rendering all the time
- Opening modal from click of a different button from parent component