简体   繁体   English

如何使用 React Dev Tools 在状态树中找到钩子的状态?

[英]How do I find state of hooks in the state tree with React Dev Tools?

On this React page we have many hooks and many context providers.在这个 React 页面上,我们有许多钩子和许多上下文提供者。 One of the hooks I care about:我关心的钩子之一:

const { branchDetails, branchLoadError } = useBranchValidator();

Inside of it there's some state along with a couple of useEffect s:在其中有一些状态以及几个useEffect s:

  const [branchId, setBranchId] = useState(queryParams.branchId ?? '');

I'd like to see the branchId value with React Dev Tools.我想用 React Dev Tools 查看branchId值。 I started with the assumption that the state for this hook would be inside the nearest context provider (image below) but it's not in there.我开始假设这个钩子的状态将在最近的上下文提供者中(下图),但它不在那里。

Is it possible to see the state in a React hook with the Dev Tools?是否可以使用开发工具查看 React 挂钩中的状态?

反应开发工具上下文

In the React Devtools sidebar, the hooks in use are displayed in a different section beneath the props , by default.在 React Devtools 侧边栏中,使用中的钩子默认显示在props下方的不同部分中。

React DevTools 显示组件上正在使用的钩子

They are not displayed with their assigned variable names in React DevTools, but which state value they are is usually inferable from context.在 React DevTools 中,它们不会与分配的变量名称一起显示,但通常可以从上下文中推断出它们的状态值。 As per React Dev Tools does not show Component Names or State Variable Names , clicking on the magic wand icon in the top right of the hooks section can display this information, but it can be resource-intensive and result in timeouts when used on complex components like context providers, in my personal experience.根据React Dev Tools 不显示 Component Names 或 State Variable Names ,单击hooks部分右上角的魔术棒图标可以显示此信息,但它可能会占用资源并导致在复杂组件上使用时超时根据我的个人经验,就像上下文提供者一样。

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

相关问题 如何使用 React 钩子中的表单进行 console.log 状态? - How do I console.log state with forms in React hooks? 如何通过钩子更新反应中的对象状态 - How do I update an object state in react via hooks React hooks:如何使用 useState() 更新嵌套对象的状态? - React hooks: How do I update state on a nested object with useState()? 反应钩子:如何使用 object 更新 state - React hooks: How do I update state with object 如何使反应 redux state 信息在开发工具上不可用? - How to make react redux state information unavailable on dev tools? 如何在反应钩子中访问以前的 state 其中 state 是一个字符串值 - How do I access the previous state in react hooks which the state is a string value 使用钩子时如何重新渲染表单值和状态值,并且在不同的组件中更改状态 - How do I rerender form value and state value when using hooks and state is changed in a different component in react 如何在 React Hooks 中将两个 state 合并到单个 state 中? - How can i merge two state into single state in React Hooks? Chrome Dev Tools 显示所有名为“State”的 useState 钩子 - Chrome Dev Tools Shows all useState hooks with the name 'State' React state 在开发工具中更新 - 但不是在 JS 中? - React state updating in dev tools - but not in JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM