I try to use react with hooks. I have this state:
const [state, setState] = useState<State>({
titel: "",
somethingElse: "",
tabledata: [],
});
I have two useeffect:
// componentDidMount, Initial variable deklaration
useEffect(() => {
//Do something
//Set initial states
setState({
...state,
titel: props.titel,
somethingElse: props.somethingElse,
})
}, []);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
//Do something and generate tabledata
let tabledata ....
//Set tabledata
setState({
...state,
tabledata: tabledata,
})
}, [props.taenzer]);
Now I have the behavior, that the second useeffect is overwriting the first useeffect setState command. My variable titel and somethingElse is always empty.
Now I could change my deklaration of state, something in this way:
const [titel, setTitel] = useState<>({
titel = ""
});
const [somethingElse, setSomethingElse] = useState<>({
somethingElse = ""
});
But this makes the whole unclear and it is not so easy to set the state of several variables in one time like we could with setState(...state, titel="Hello", somethingElse="Else")
Any other possibility?
useReducersetStateand the last one will "stick" but most likely both will be reading the same initial state.