DEV Community

Discussion on: Set a callback to setState(useState) in React funtional Component

Collapse
 
thejajibhee profile image
JAJI ⚡️⚡️ || BHEE INC.

Hi Prakash, so are you saying we write two effects or use one effect for both the setting of state and calling API?

useEffect(() => {
setFilterObj({ ...props.something});
}, [props.something]);

useEffect(() => {
fetchData(currentPage);
}, [filterObj])

OR
useEffect(() => {
setFilterObj({ ...props.something});
fetchData(currentPage);
}, [props.something, filterObj ]);

Collapse
 
chandra profile image
Chandra Prakash Tiwari

Actually, this is an alternate solution for calling something after setting the state in a functional component. You can use useEffect() more than once.