React文档说:
React is pretty flexible but it has a single strict rule: all React components must act like pure functions with respect to their props.
这是为什么?
我猜想,如果您直接更改props的值,则组件不会重新渲染,这就是我们必须使用的原因setState。但我仍然不了解其背后的原因。为什么组件在其道具方面必须像纯函数一样?
当我在extraReducers切片内部添加特定操作时,出现以下错误:
Uncaught TypeError: Cannot read properties of undefined (reading 'type')。
例子:
import { createSlice } from '@reduxjs/toolkit'
export const mySlice = createSlice({
name: 'name',
initialState,
extraReducers: (builder) => {
// If I console.log action2 here, it turns out to be undefined
builder.addCase(action1, () => ...)
builder.addCase(action2, () => ...) // when I add this specific action I get the error.
},
reducers: {
...
},
})
Run Code Online (Sandbox Code Playgroud)
action2的定义类似于action1另一个文件中的 :
import { createAction } from '@reduxjs/toolkit' …Run Code Online (Sandbox Code Playgroud)