Reactjs 如何为useEffect中的每个单独道具调用单独的代码
我想使用useEffect方法,当任何道具被更改时调用它,但不是所有的代码,只有一个专门用于此道具的代码 我想是这样的。。。 在这一刻,所有的代码都被称为whed one,two或threeReactjs 如何为useEffect中的每个单独道具调用单独的代码,reactjs,react-hooks,use-effect,Reactjs,React Hooks,Use Effect,我想使用useEffect方法,当任何道具被更改时调用它,但不是所有的代码,只有一个专门用于此道具的代码 我想是这样的。。。 在这一刻,所有的代码都被称为whed one,two或three const SomethingComponent = (props: any) => { const {one, two, three} = props; useEffect(() => { if(something for one props){ c
const SomethingComponent = (props: any) => {
const {one, two, three} = props;
useEffect(() => {
if(something for one props){
code for one
}
if(something for two props){
code for two
}
if(something for three props){
code for three
}
}, [one, two, three]);
}
您可以使用不同的钩子,并为每个钩子指定一个依赖项。这样,当依赖项更改时,只有特定的
useffect
才会触发:
const SomethingComponent = (props: any) => {
const { one, two, three } = props
useEffect(() => {
// code for one
}, [one])
useEffect(() => {
// code for two
}, [two])
useEffect(() => {
// code for three
}, [three])
return null;
}
完美的谢谢;)我想我只能使用useffect
一次