【问题标题】:Redux Form initialvlaues not found未找到 Redux 表单初始值
【发布时间】:2020-03-07 02:31:19
【问题描述】:

我不明白为什么这段代码不起作用......当我转到这个组件时,他没有用初始值渲染,我不明白为什么......

我一直在查看 redux-form 文档,但找不到如何操作

import { Field, reduxForm } from 'redux-form';
import { connect } from 'react-redux';

const fieldNombre = (props) => {

    return (
        <TextInput
            placeholder="Field nom"
            /*onChangeText={props.input.onChange}
            value={props.input.value}*/
            style={{borderWidth: 1, borderColor: 'white', color: 'white'}}
        />
    );
};

class EditForm2 extends Component {

    render() {
        console.log('this.props.initialValues');
        console.log(this.props.initialValues);
        return (
            <View>
                <Field name="firstName" component={fieldNombre} />
                <WhiteText>Redux Form</WhiteText>
                <Button
                    title="Registrar"
                    onPress={this.props.handleSubmit((values) => {
                        console.log(values);
                    })}
                />
            </View>
        );
    }
};

const mapStateToProps = (state) => {
  return {
    initialValues: {
      firstName: 'aaaaa',
      lastName: 'bbbbb',
      email: 'cccccc'
    }
  }
}

export default connect(mapStateToProps)(reduxForm({ form: 'EditForm2', enableReinitialize: true})(EditForm2))```

【问题讨论】:

  • 你是说console.log(this.props.initialValues); 已经显示未定义?还是这个 console.log 显示了值,但它们在输入字段中不可见?
  • @Daniel 这个 console.log 显示了值,但它们在输入字段中不可见
  • @Cookie 你能检查我的答案吗?如果您有任何问题,请随时给我写信。

标签: react-native redux redux-form


【解决方案1】:

在您的自定义组件fieldNombre 中,您应该使用props.input.value 并将其传递给&lt;TextInput value={props.input.value} /&gt;

这是来自redux-form 文档的完整示例:https://redux-form.com/8.2.2/docs/api/field.md/#1-a-normal-component

【讨论】:

  • 您的回答对我有用,但现在我遇到了与此问题相关的其他问题。在这里观看stackoverflow.com/questions/58818669/…
  • @Cookie 如果这个答案帮助你渲染了初始值,你可以考虑接受它。稍后,我将审查您的下一个问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-10-24
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
相关资源
最近更新 更多