在完成ReactJ中功能组件的一组状态变量设置后,如何调用功能

发布于 2025-02-09 19:37:00 字数 479 浏览 3 评论 0原文

function check(){
const [variable, setvariable1] = useState(0);
const [variable2, setvariable2] = useState(0);
const [variable3, setvariable3] = useState(0);

const last_func = () => {
console.log("call this function")
}

const mainfunction = () =>{
setvariable(1);
setvariable2(1);
setvariable3(1);
last_func(); //this function needs to be called after completing above all state variables
}
}

我想在完成所有状态变量的设置为1之后致电last_func

function check(){
const [variable, setvariable1] = useState(0);
const [variable2, setvariable2] = useState(0);
const [variable3, setvariable3] = useState(0);

const last_func = () => {
console.log("call this function")
}

const mainfunction = () =>{
setvariable(1);
setvariable2(1);
setvariable3(1);
last_func(); //this function needs to be called after completing above all state variables
}
}

i want call the last_func after completing the setting of all state variables to 1 then only i have to call that last_func

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋意浓 2025-02-16 19:37:00
const mainfunction = () =>{
 setvariable(1);
 setvariable2(1);
 setvariable3(1);

 if(variable !== 0 && variable2 !== 0 && variable3 !== 0) last_func()

 //or if you know that variables will be equal to 1
 if(variable === 1 && variable2 === 1 && variable3 === 1) last_func()
}

另外,如果您想在所有变量为'0之后自动运行last_fun,则可以使用使用效率:

useEffect(()=>{
mainfunction();
}, [variable, variable2, variable3])
const mainfunction = () =>{
 setvariable(1);
 setvariable2(1);
 setvariable3(1);

 if(variable !== 0 && variable2 !== 0 && variable3 !== 0) last_func()

 //or if you know that variables will be equal to 1
 if(variable === 1 && variable2 === 1 && variable3 === 1) last_func()
}

also if you want to run last_fun automatically after all variables are >0 you can use useEffect:

useEffect(()=>{
mainfunction();
}, [variable, variable2, variable3])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文