Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何为useEffect中的每个单独道具调用单独的代码_Reactjs_React Hooks_Use Effect - Fatal编程技术网

Reactjs 如何为useEffect中的每个单独道具调用单独的代码

Reactjs 如何为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

我想使用useEffect方法,当任何道具被更改时调用它,但不是所有的代码,只有一个专门用于此道具的代码

我想是这样的。。。 在这一刻,所有的代码都被称为whed one,two或three

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
一次