我的用例是,我想从我的状态中清除一些数据。为了清除状态,我不需要任何api调用,因此,我没有为它写任何传奇。
Action.js
export const remove = (payload: string) => {
console.log('in here', payload)
return {
type: types.REMOVE,
payload
}
}
Reducer.js
const selectReducer = (
state: any = initialState2,
action: any
) => {
console.log('selectWorkspacesReducer')
const { type, payload, error } = action;
switch (type) {
case types.REMOVE: {
console.log('initialState2', initialState2);
console.log('action', action);
return {
...state,
...initialState2,
p: 1
}
}
default:
return state;
}
}
组件
const TreeView = (props: {remove: Function }) => {
const {remove} = props;
const handleClick = (payload: string) => {
console.log('payload -> ', payload)
remove(payload);
}
};
return (
<div className="tree-view">
<div className="column" onClick={() => handleClick('test')}>Submit</div>
</div>
);
}
export const mapDispatchToProps = {
remove
}
export default connect(null, mapDispatchToProps)(TreeView);
这些文件中写入的所有其他减速器和操作都可以工作。在删除的情况下,我得到的日志是用action.js写的,但不是用reducer写的。
我认为您需要调用remove
函数,您需要将action作为参数传递给dispatch函数。
dispatch(remove(payload));
嗨,我的朋友,它工作:(
export const mapDispatchToProps = (dispatch) => {
return {
remove: (payload)=> dispatch(remove(payload))
}
}
请阅读此api:([https://react-redux.js.org/api/connect#mapdispatchtoprops-object——dispatch ownprops——object][1]
(如果你有任何问题,问一下?([1] :https://react-redux.js.org/api/connect#mapdispatchtoprops-object—dispatch ownprops—object