I have variable such as
const [render,setRender] = useState(false);
then, I can use call back like this.
function clickBtn(){
setRender(true);
}
useEffect(() =>{
//do something after render is finished.
},[render]);
However I want to change the method by case.
function clickBtn(){
setRender(true);
}
function clickBtn2(){
setRender(true);
}
useEffect(() =>{
//I want to know which Btn is clicked and change the method.
//doSomething1();
//doSomething2();
},[render]);
or like this
function clickBtn(){
await setRender(true);
doSomething1();
}
function clickBtn2(){
await setRender(true);
//doSomething2();
}
useEffect(() =>{
},[render]);
What is possible and how is the best practice for this method?
2
Answers
Consider setting a value for
render
that is different per function:You need a flag and this can be done using
ref
.Sandbox