[英]There is a way to pass a action via props? To make re-usable components with react-redux
I would like to make a component that loads actions and states dynamically so that I can reuse it in other parts of the code.我想制作一个动态加载动作和状态的组件,以便我可以在代码的其他部分重用它。
I'm trying this way but the action is firing before sending to component:我正在尝试这种方式,但该操作在发送到组件之前触发:
function ClientsHeader(props) {
const action = Actions.searchClients();
return (
<SearchHeader data={data} campoPesquisa={campoPesquisa} action={action}/>
);
}
export default ClientsHeader;
On children:关于儿童:
function SearchHeader(props) {
const dispatch = useDispatch();
const action = props.action;
return;
<div className="flex flex-1 w-full items-center justify-between">
<Input
placeholder="Search"
onChange={event => dispatch(action(event))}
/>
</div>;
}
There is a way to call a function name with variables?有没有办法用变量调用 function 名称? Because that would solve this.
因为那会解决这个问题。
Please don't be rude to my stupidity, I'm new to this:D请不要对我的愚蠢无礼,我是新手:D
The error is from the line const action = Actions.searchClients();
错误来自
const action = Actions.searchClients();
This actually calls the function searchClients
.这实际上调用了 function
searchClients
。
It should rather be const action = () => Actions.searchClients();
它应该是
const action = () => Actions.searchClients();
It's because you are calling Actions.searchClients in ClientsHeader component, you just need to pass the reference as这是因为您在 ClientsHeader 组件中调用 Actions.searchClients,您只需将引用传递为
const action = Actions.searchClients;
Note: I think you should import the action in the component directly,注意:我认为你应该直接在组件中导入动作,
Hope it helps希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.