简体   繁体   English

如何使用一个自定义钩子在两个远程组件之间共享数据

[英]How to share data between two remote components with one custom hook react

I have two components which are located in different position in the hierarchy.我有两个组件位于层次结构中的不同 position 中。 One component has data which should be used in another component.一个组件具有应该在另一个组件中使用的数据。 I was trying to achieve this with custom hook.我试图用自定义钩子来实现这一点。 When I used in components I'm getting false, I want it to be true because I set it to true from component 1 so component 2 should get true.当我在组件中使用时,我得到了错误,我希望它为真,因为我从组件 1 将其设置为 true,因此组件 2 应该为 true。 I don't know little bit confused on custom hooks.我不知道对自定义挂钩有点困惑。

Custom Hook自定义挂钩

export const useShareData = () => {
  const [data, setData] = useState<boolean>(false)

  const setValue = (value: boolean) => {
    setData(value)
  }
  return [data, setValue]
}

Component 1组件 1

const [data, setData] = useShareData()
const dataToBeShared = true
useEffect(()=>{
   setData(dataToBeShared)
},[dataToBeShared])

Component 2组件 2

const [data] = useShareData()
console.log(data)

In component 2 the value is always false.在组件 2 中,该值始终为 false。

Creating a custom hook doesn't let data be shared, it just lets code be shared.创建自定义钩子不会让数据被共享,它只是让代码被共享。 While component 1 is rendering, if it calls const [data, setData] = useState<boolean>(false) , it creates a state variable for component 1. This is the case whether it's written inline or extracted to a helper function (aka, a custom hook).当组件 1 正在渲染时,如果它调用const [data, setData] = useState<boolean>(false) ,它会为组件 1 创建一个 state 变量。无论是内联写入还是提取到帮助程序 function (又名,一个自定义钩子)。 Similarly, when component 2 calls useState, it creates state for component 2. The two states are not shared.同样,当组件 2 调用 useState 时,它会为组件 2 创建 state。这两个状态不共享。

The standard approach for your issue is to lift state up to whatever component is the common ancestor of these two components, and then pass it down.您的问题的标准方法是将 state 提升到这两个组件的共同祖先的任何组件,然后将其传递下去。 If the common ancestor is nearby, props can be used;如果共同祖先在附近,可以使用道具; if it's far away, context becomes a more attractive option.如果距离很远,上下文将成为更有吸引力的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 React Hook 触发两个组件之间的函数? - How can I trigger functions between two components with React Hook? 如何在 React Router 和 outlet 中不使用高阶函数的情况下在两个组件之间共享数据 - How to share data between two components without using higher order functions in React Router and outlet 如何在同一个存储库中的两个Meteor应用之间共享React组件? - How to share React components between two Meteor apps in the same repository? 无法在反应中的两个组件之间共享状态 - not able to share state between two components in react 是否可以使用 React 中的 useState() 钩子在组件之间共享状态? - Is it possible to share states between components using the useState() hook in React? 如何在 React 中的钩子之间共享数据(协调松散相关的组件) - How to share data between hooks in React (coordinating loosely related components) 如何连接我的两个组件以在反应中共享数据 - How can I connect two of my components to share data in react 在React组件之间共享数据而没有关系? - Share data between React components with no relation? React Native 中组件之间的道具和共享数据 - Props and share data between components in React Native 使用 React Hook 与其他组件共享 API 数据,NO REDUX - Share API data with other components by using React Hook, NO REDUX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM