gpt4 book ai didi

reactjs - 尝试将调度函数包装在react redux中

转载 作者:行者123 更新时间:2023-12-02 19:31:42 24 4
gpt4 key购买 nike

嗨,最近我遇到了 useDispatch 钩子(Hook),它应该为我提供 mapDispatchToProps 的替代方案,但我发现这样做非常重复 () => dispatch(action(args))在每次 onPress 中,所以我开始思考一些通用的东西。我的目标是制作一个使用 useDispatch() 的钩子(Hook)并包装它获取和返回的函数 () => dispatch(theWrappedAction(theActionArgs))例如,如果我有一个操作 upCounterActionCreator如下:

export const upCounterActionCreator = (count: number = 1): AppActions => {
const action: UpCounterAction = {
type: 'UP_COUNTER',
count
};
return action;
};

我的目标是做这样的事情:
const [upAfterDispatch] = useActions(upCounterActionCreator);

然后我可以做:

<Button onPress={upAfterDispatch(1)} title='+' />

我尝试做的事情如下:

export const useActions = (...actions: ((...args: any) => AppActions)[]) => {
const dispatch = useDispatch<Dispatch<AppActions>>();
const actionsWithDispach: ((...args: any) => () => (...args: any) => AppActions)[] = [];

actions.forEach(action => {
actionsWithDispach.push((...args: any) => () => (...args: any) => dispatch(action(...args)));
});
return actionsWithDispach;

};

要将包装好的函数放在 onPress 上,我需要这样做

<Button onPress={upAfterDispatch(1)()} title='+' /> - 调用它,这不是一个好的选择。然后,当我调用它时,操作确实正在被调度,但是当我在有效负载上调试时,我有一个包含计数的对象,如下所示:这是一个类 -

enter image description here

我做错了什么?我需要做什么才能:

  1. 获取数字 1(操作负载中发送的计数参数)而不是类

  2. 调用 useActions 返回的函数,而不是像这样调用 onPress={upAfterDispatch(1)()

** 我认为 args 中收到的对象是 React Native onPress 事件,如何避免它覆盖我的 count 参数?先谢谢了!

最佳答案

我认为这就是你想做的:

export const useActions = (...actions: ((...args: any) => AppActions)[]) => {
const dispatch = useDispatch<Dispatch<AppActions>>();
const actionsWithDispach: ((...args: any) => () => (...args: any) => AppActions)[] = [];

actions.forEach(action => {
actionsWithDispach.push((...args: any) => () => dispatch(action(...args)));
});
return actionsWithDispach;
};

您添加了额外的 (...args: any) =>,但使用上面的代码您可以执行 onClick={theAction(1)}

const { Provider, useDispatch, useSelector } = ReactRedux;
const { createStore } = Redux;
const initialState = {
count: 0,
};
const reducer = (state, { type, payload }) => {
if (type === 'UP') {
return { count: state.count + payload };
}
return state;
};
const store = createStore(
reducer,
{ ...initialState },
window.__REDUX_DEVTOOLS_EXTENSION__ &&
window.__REDUX_DEVTOOLS_EXTENSION__()
);
//action
const add = (howMuch) => ({ type: 'UP', payload: howMuch });
const useAction = (action) => {
const dispatch = useDispatch();

return React.useMemo(
() => (...args) => () => dispatch(action(...args)),
[action, dispatch]
);
};
const Button = React.memo(function Button({ up, howMuch }) {
const rendered = React.useRef(0);
rendered.current++;
return (
<button onClick={up(howMuch)}>
Rendered: {rendered.current} times, add {howMuch}
</button>
);
});

const App = () => {
const up = useAction(add);
const count = useSelector((state) => state.count);
return (
<div>
<h2>count:{count}</h2>
<Button up={up} howMuch={1} />
<Button up={up} howMuch={1} />
<Button up={up} howMuch={1} />
</div>
);
};

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.5/redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/7.2.0/react-redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reselect/4.0.0/reselect.min.js"></script>


<div id="root"></div>

关于reactjs - 尝试将调度函数包装在react redux中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61703101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com