Skip to main content

故障排除

¥Troubleshooting

Reactiflux Discord 社区#redux 通道 是我们的官方资源,可解答与学习和使用 Redux 相关的所有问题。Reactiflux 是一个闲逛、提问和学习的好地方 - 来加入我们吧!

¥The #redux channel of the Reactiflux Discord community is our official resource for all questions related to learning and using Redux. Reactiflux is a great place to hang out, ask questions, and learn - come join us!

你还可以使用 #redux 标签 询问有关 Stack Overflow 的问题。

¥You can also ask questions on Stack Overflow using the #redux tag.

我的观点没有更新!

¥My views aren’t updating!

简而言之,

¥In short,

  • Reducer 永远不应该改变状态,它们必须返回新对象,否则 React Redux 将看不到更新。

    ¥Reducers should never mutate state, they must return new objects, or React Redux won’t see the updates.

  • 确保你确实正在调度操作。例如,如果你有一个像 addTodo 这样的动作创建器,则仅调用导入的 addTodo() 函数本身不会执行任何操作,因为它只返回一个动作,但不会分派它。你需要调用 dispatch(addTodo())(如果使用 hooks API)或 props.addTodo()(如果使用 connect + mapDispatch)。

    ¥Make sure you are actually dispatching actions. For example, if you have an action creator like addTodo, just calling the imported addTodo() function by itself won't do anything because it just returns an action, but does not dispatch it. You either need to call dispatch(addTodo()) (if using the hooks API) or props.addTodo() (if using connect + mapDispatch).

在上下文或属性中找不到 "store"

¥Could not find "store" in either the context or props

如果你有上下文问题,

¥If you have context issues,

  1. 页面上有 确保你没有重复的 React 实例

    ¥Make sure you don’t have a duplicate instance of React on the page.

  2. 确保你的项目中没有 React Redux 的多个实例/副本。

    ¥Make sure you don't have multiple instances/copies of React Redux in your project.

  3. 确保你没有忘记将根或其他祖级组件封装在 <Provider> 中。

    ¥Make sure you didn’t forget to wrap your root or some other ancestor component in <Provider>.

  4. 确保你运行的是最新版本的 React 和 React Redux。

    ¥Make sure you’re running the latest versions of React and React Redux.

不变违规:addComponentAsRefTo(...):只有 ReactOwner 才能拥有 ref。这通常意味着你正在尝试向没有所有者的组件添加引用

¥Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you’re trying to add a ref to a component that doesn’t have an owner

如果你使用 React for web,这通常意味着你有 重复反应。请按照链接的说明修复此问题。

¥If you’re using React for web, this usually means you have a duplicate React. Follow the linked instructions to fix this.

我在单元测试中收到有关 useLayoutEffect 的警告

¥I'm getting a warning about useLayoutEffect in my unit tests

如果 useLayoutEffect 使用 "在服务器上",ReactDOM 会触发此警告。React Redux 尝试通过检测它是否在浏览器上下文中运行来解决该问题。默认情况下,Jest 定义了足够的浏览器环境,React Redux 认为它在浏览器中运行,从而导致这些警告。

¥ReactDOM emits this warning if useLayoutEffect is used "on the server". React Redux tries to get around the issue by detecting whether it is running within a browser context. Jest, by default, defines enough of the browser environment that React Redux thinks it's running in a browser, causing these warnings.

你可以通过为单个测试文件设置 @jest-environment 来防止出现警告:

¥You can prevent the warning by setting the @jest-environment for a single test file:

// my.test.jsx
/**

* @jest-environment node
*/

或者通过全局设置:

¥Or by setting it globally:

// package.json
{
"name": "my-project",
"jest": {
"testEnvironment": "node"
}
}

https://github.com/facebook/react/issues/14927#issuecomment-490426131

¥See https://github.com/facebook/react/issues/14927#issuecomment-490426131