我已经开始在功能组件中使用 redux-toolkit 切片器,(来自 react-redux 示例的示例)
切片机:
export const counterSlice = createSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
increment: state => {
state.value += 1;
},
decrement: state => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload;
},
},
});
Run Code Online (Sandbox Code Playgroud)
在组件中使用:
const count = useSelector(selectCount);
const dispatch = useDispatch();
return (
<button
className={styles.button}
aria-label="Increment value"
onClick={() => dispatch(increment())}
>
)
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在类组件中使用这个切片器,因为我不能在它们内部使用钩子。我试过使用 connect(来自 redux),但我找不到一种方法将切片器中的动作和选择器“缝合”到我的组件。我也找不到任何关于此的文档。