假设我有这样一个东西,函数返回值和setter函数,我如何正确实现setter函数来更新返回值,每次调用它?(比如 useState 的返回值和 updater 函数)
const myFunction = (initialValue) => {
let value = initialValue;
const setterFunction = (newValue) =>{
value= newValue;
}
forceRerender() //function that forces re-renders
return [value,setterFunction];
}
const [myValue,updaterFunc] = myFunction('someValue');
updaterFunc('newValue'); // myValue's new Value should be 'newValue'