React(本机)通过道具获取功能/动作名称
我正在使用React Antial和Redux。现在,我想创建一个动态组件,例如下拉组件。我通过道具通过状态可以正常运行,但是要使此组件重新使用,我必须调用此状态的正确调度函数。我不确定这是否是正确的方法。我只使用一个星期的反应;)
为简单起见,简单的文本字段组件:
import React, {Component} from 'react';
import {Button, StyleSheet, Text, TextInput, View} from 'react-native';
import allActions from '../../actions/allActions';
import {useDispatch, useSelector} from 'react-redux';
function Name (props) {
const dispatch = useDispatch();
const componentState = props.state;
/**
* Save the new user to the state
* @param text
*/
const handleChange = (text) => {
// here I want to call the action dynamically
// e.g. allActions.userActions.MY_VARIABLE_PASSED_THROUGH_PROPS
dispatch(allActions.userActions.setUser(text));
}
return (
<View>
<TextInput
style={styles.input}
defaultValue={componentState}
onChangeText={handleChange}
/>
</View>
);
}
export default Name;
当我对组件进行分配时,我通过状态
<Name state={ store.getState().user.name } action={my_action_to_call}/>
编辑:这是我的UserAction
import * as actions from '../actionTypes';
const setUser = (userObj) => {
return {
type: actions.SET_USER,
payload: userObj
}
}
const setGender = (userObj) => {
return {
type: actions.SET_GENDER,
payload: userObj
}
}
export default {
setUser,
setGender
}
I am using react native and redux. Now I want to create a dynamic component, for example a dropdown component. I pass a state via props which works fine but to make this component re-usable I have to call the right dispatch function for this state. I am not sure if that is the right way to do. I am only working with react for a week now ;)
For the sake of simplicity a simple text field component:
import React, {Component} from 'react';
import {Button, StyleSheet, Text, TextInput, View} from 'react-native';
import allActions from '../../actions/allActions';
import {useDispatch, useSelector} from 'react-redux';
function Name (props) {
const dispatch = useDispatch();
const componentState = props.state;
/**
* Save the new user to the state
* @param text
*/
const handleChange = (text) => {
// here I want to call the action dynamically
// e.g. allActions.userActions.MY_VARIABLE_PASSED_THROUGH_PROPS
dispatch(allActions.userActions.setUser(text));
}
return (
<View>
<TextInput
style={styles.input}
defaultValue={componentState}
onChangeText={handleChange}
/>
</View>
);
}
export default Name;
I pass the state when I instatiate the component
<Name state={ store.getState().user.name } action={my_action_to_call}/>
EDIT: This is my userAction
import * as actions from '../actionTypes';
const setUser = (userObj) => {
return {
type: actions.SET_USER,
payload: userObj
}
}
const setGender = (userObj) => {
return {
type: actions.SET_GENDER,
payload: userObj
}
}
export default {
setUser,
setGender
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了:
组件调用
,这是处理功能:
I figured it out:
Component call
and here is the handleChange function:
不用担心!没错。为了在 React 中制作动态组件,你必须通过 props 传递值和函数。
您的操作是函数,因此您可以轻松地传递函数并在组件中调用它。如果我是你,我肯定会这么做。喜欢:
Don't worry! That is right. for making dynamic components in react you do have to pass values and fuction via props.
Your actions are fuctions, so easily you can pass your function and call it in your component. If I were you, I would definitely do that. like: