I have an application that works like this. State contains a target. The user can click on a save button to open a file picker and select a target. The intended behavior is that whenever the user selects a target the application performs a save.
The code below almost works. But unfortunately the save effect does not happen when the user selects the same target as was previously in state.
const [state, dispatch] = useReducer(reducer, {
target: "",
});
const setTarget = async () => {
// user interacts with a file picker
dispatch({ type: SET_TARGET, target});
}
useEffect(
function save() {
if (state.target) {
// some side effects happen
}
},
[state.target]
);
What is a good way to solve this issue using the world of React and Hooks?