score:8
Accepted answer
useEffect
takes a second argument which denotes when to execute the effect. You can pass in the state value to it so that it executes when state updates. Also you can have multiple useEffect
hooks in your code
const [propA, setPropA] = useState(props.propA);
const [propB, setPropB] = useState(props.propB);
useEffect(() => {
props.onChangePropA(propA);
}, [propA]);
useEffect(() => {
props.onChangePropB(propB);
}, [propB]);
<div>
<button onClick={e => {setPropA(e.target.value)}}>Prop A</button>
<button onClick={e => {setPropB(e.target.value)}}>Prop B</button>
</div>
Source: stackoverflow.com
Related Query
- How to notify parent component of property change when using react hooks?
- How to prevent child component from re-rendering when using React hooks and memo?
- React 16: Call children's function from parent when using hooks and functional component
- how to change background color when I click the button using react hooks
- How to change the state of the particular component when it is clicked, using useState in react js?
- How to reset child component from parent using React Context and Hooks
- How to get parent props in child component when using styled component in react
- How to loop a component when I click a h3 tag in react using Hooks
- How to render component conditionally when leaf value of global state changes using react hooks
- How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js?
- How to pass Children component state to Parent component state in React using Hooks
- How to change button background color when I click the button in React using Hooks
- How to pass an Array from Child component to Parent component using hooks in react
- How do you change a style of a child when hovering over a parent using MUI styles?
- React - How to detect when all sub-components of a parent component are visible to the user?
- How to pass the match when using render in Route component from react router (v4)
- How to get a React Component reference to change its class using classList?
- React Redux - How to dispatch an action on componentDidMount when using mapDispatchToProps in a connected component
- how and when to call a react component methods after state change from redux
- React Parent Component completely reconstructed when using Redirect
- Cannot read property of undefined when using react hooks
- How can I spread props to a React component that uses exact props when using Flow?
- How should unsubscribe be handled in a react component when using redux?
- how to change image src using props with styled component and react
- How do I check if user is 'logged in' in a react component when using laravel authentication?
- React - How do i force child components to re render when parent state component changes?
- Trigger child function from parent component using react hooks
- How to solve closures issue when using React functional component with React.memo?
- React - How change state of single component when there are multiple instance of same component?
- How to re-render react component when a property changes
More Query from same tag
- Can a handler method access another handler methods property?
- Cors origin React Api plateform
- Add ids of Favorite products to localstrorage (array)
- Cannot read properties of undefined (reading 'find')
- react hook conditional useState - JSON.parse(...) is null
- Avoiding stale state in double useEffect?
- React passing state Objects to child component
- How to render custom Grid item property in react component with TypeScript
- VM892:1 Uncaught SyntaxError: Unexpected token e in JSON at position 0
- React.js: create-react-app working but returns this type of error
- Importing CSS files in Isomorphic React Components
- Split auth-workflow example(ReactJS) on components
- Dispatching first runs undefined and then resolves in Redux Thunk
- How to wrap a part of text fetched from array of object in a span tag?
- In React, how to show data from API in a HTML table?
- How to use react context api with getDerivedStateFromProps?
- Build Failing with React Firebase Hooks and Vite
- How to set a interceptor for an action getting fired in redux
- React CSSTransitionGroup deleted item shifted to end
- Clicking Image to link to another website React.JS
- how to use redux-saga with async callback function
- Redux store shape using spread
- Translating from React to Mithril 2? (a Meiosis example)
- Fetch API using Saga didn't give any response
- How to make a stylesheet accessible to only one component react.js
- Combine Reducer without Redux
- How to do nested conditional rendering in React?
- How to check added custom errors are working in videojs player
- Notification when user try to close browser or tab
- State is not getting initialized to the initial state