Redux form defaultValue

后端 未结 3 533
别那么骄傲
别那么骄傲 2021-01-01 11:43

How to set defaultValue to input component?


<         


        
3条回答
  •  萌比男神i
    2021-01-01 12:32

    On redux forms you can call initialize() with an object of values like so:

    class MyForm extends Component {
      componentWillMount () {
        this.props.initialize({ name: 'your name' });
      }
    
      //if your data can be updated
      componentWillReceiveProps (nextProps) {
        if (/* nextProps changed in a way to reset default values */) {
          this.props.destroy();
          this.props.initialize({…});
        }
      }
    
      render () {
        return (
          
    ); } } export default reduxForm({})(MyForm);

    This way you can update the default values over and over again, but if you just need to do it at the first time you can:

    export default reduxForm({values: {…}})(MyForm);
    

提交回复
热议问题