1

I would like to make useEffect method, which is calling when any prop is changed but not all the code, just a single one dedicated for this props

I imagine something like that ... In this moment all the code is called whed one, two or three is changed.

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]);
}
4

1 回答 1

3

您可以使用不同的钩子,并为每个钩子提供一个依赖项。这样,useEffect当依赖项发生变化时,只有特定的才会触发:

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;
}
于 2019-09-18T18:41:20.553 回答