大佬教程收集整理的这篇文章主要介绍了在 ReactElements 列表中访问 Redux Store,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个这样的列表:
const form = [
<input title="what is your age?" onsubmit={} />,// this onsubmit sets age
<MultipleChoice title="what is the weather today?" onsubmit={} />,// this onsubmit sets weather
<input title="what is the brand of your rain coat?" onsubmit={} /> // this onsubmit sets rainCoatBrand
]
注意:上面的这个列表不是在 React 组件内部定义的。我也有这样的店:
age: number;
weather: String;
rainCoatBrand: String;
对于 form
中的每个元素,如何设置 onsubmit
方法以使用我想要的值更新存储?谢谢!
您可以使用 store.dispatch() 从 React 组件外部实现此目的。请注意,要使其正常工作,您需要在某处定义 reducers 以处理分派的操作并相应地更新存储。
import { store } from './storepath';
const form = [
<Input
title="what is your age?"
onSubmit={() => store.dispatch({ // redux action payload })}
/>,<MultipleChoice
title="what is the weather today?"
onSubmit={() => store.dispatch({ // redux action payload })}
/>
<Input
title="what is the brand of your rain coat?"
onSubmit={() => store.dispatch({ // redux action payload })}
/>
]
要从输入中访问实际值,您需要修改输入组件本身来处理这个问题。
例如
export const MultipleChoice = (props) => {
const [value,SETVALue] = useState();
...
return <div title={props.titlE} onSubmit={() => props.onSubmit(value)}/>
}
const form = [
<MultipleChoice
title="what is the weather today?"
onSubmit={(payload) => store.dispatch({ type: 'updatE_WEATHER',payload })}
/>,...
]
以上是大佬教程为你收集整理的在 ReactElements 列表中访问 Redux Store全部内容,希望文章能够帮你解决在 ReactElements 列表中访问 Redux Store所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。