简体   繁体   中英

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. 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:

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

I'd like to see the branchId value with React Dev Tools. 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?

反应开发工具上下文

In the React Devtools sidebar, the hooks in use are displayed in a different section beneath the props , by default.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM