【问题标题】:React custom hook useState initialization value not update through outside props反应自定义钩子useState初始化值不通过外部道具更新
【发布时间】:2023-04-01 00:20:01
【问题描述】:

我有一个使用useState 具有语言环境状态值的自定义钩子,并且我从外部道具设置初始值,但是当我的道具更改时,我的内部状态值没有得到更新,此外我不太明白,我的组件或应用程序生命周期中有多少个自定义钩子实例每次被触发?

下面是代码示例:

// custom hook
const useCustomHook = (initValue) => {
   const [isFetching, setIsFetching] = useState(initValue);

   useEffect(() => {
      console.log('initValue :>> ', initValue, ', isFetching :>>', isFetching);
   }, [initValue, isFetching);
}
// component
const myComponent = (props) => {
   const [shouldTrigger, setShouldTrigger] = useState(false);

   useCustomHook(shouldTrigger);

   onButtonClick = () => {
     setShouldTrigger(true);
   }
}

这是我得到的控制台日志,

// when my component mouts
'initValue :>> ', false, ', isFetching :>>', false
// when button clicked
'initValue :>> ', true, ', isFetching :>>', false

如您所见,只要我从主组件将 shouldTrigger 设置为 true,我的自定义钩子就会被调用,但是,我的自定义钩子值中的本地状态值 isFetching 仍然是 false,应该是不是每次都会从外部道具分配?我上面的两个useCustomHook 是同一个实例还是不同?如果调用了不同的实例,为什么第二个实例没有将初始值设置为“true”?

这里是代码链接 https://stackblitz.com/edit/react-yma5my?file=index.js

【问题讨论】:

    标签: react-redux react-hooks use-effect use-state


    【解决方案1】:
    1. 我不确定,但我认为这是因为如果您使用 useState,则只能定义一次此值。
    2. and is my above two useCustomHook the same instance or different 他们会有所不同。

    您可以将代码重写为

    // custom hook
    const useCustomHook = (initValue) => {
       const [isFetching, setIsFetching] = useState(initValue);
    
       useEffect(() => {
          console.log('initValue :>> ', initValue, ', isFetching :>>', isFetching);
       }, [initValue, isFetching);
      return [isFetching, setIsFetching]
    }
    // component
    const myComponent = (props) => {
       const [isFetching, setIsFetching] = useCustomHook(false);
    
       onButtonClick = () => {
         setIsFetching(true);
       }
    }
    

    更新

    我刚刚知道如何以你想要的风格写作,但我的第一个建议还是更好(我认为)

    // custom hook
    const useCustomHook = (value) => {
       const [isFetching, setIsFetching] = useState(value);
    
       useEffect(() => {
         setIsFetching(value);
       }, [value]);
    
       useEffect(() => {
          console.log('value :>> ', value, ', isFetching :>>', isFetching);
       }, [initValue, isFetching);
    }
    // component
    const myComponent = (props) => {
       const [shouldTrigger, setShouldTrigger] = useState(false);
    
       useCustomHook(shouldTrigger);
    
       onButtonClick = () => {
         setShouldTrigger(true);
       }
    }
    

    它会导致额外调用 setState 但实现基于输入道具的状态更新;

    【讨论】:

    • 嗨@boikov,谢谢你的想法,我看到人们以前做过return [isFetching, setIsFetching],但我有点混淆了这样的使用方式。由于customHook返回setIsFetching,那么在我的组件中,setIsFetching(true)的按钮点击时,它实际上是在调用我的组件实例的setIsFetching还是我的customHook实例?将set 语句作为自定义钩子的回报有什么好处?
    • 1) As the customHook returns setIsFetching, then in my component when does the button click of setIsFetching(true), was it actually calling the setIsFetching of my component instance or my customHook instance? 如果您按照我的第一个示例中的方式编写,您将使用 customHook 实例的 setIsFetching。
    • 2) 如果您返回一些 setMethods,您可以使用该钩子的数据进行操作并在某处调用它。例如,您有一个根据请求结果计算数据的钩子。可以通过 props 传递给 hook 方法,也可以在 useEffect hook 中使用,this.request().then((res) => setHookValue(res));
    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 2019-09-15
    • 1970-01-01
    • 2021-02-08
    相关资源
    最近更新 更多