Does redux-form field value can hold object instead of just a string?

后端 未结 5 1137
暖寄归人
暖寄归人 2020-12-31 18:10

Does redux-form field value can hold object instead of just a string?

Consider following example

    class SelectQuestions extends Component{
                


        
5条回答
  •  离开以前
    2020-12-31 18:37

    TL:DR

    Yes it can hold an object. Check out this example: https://codesandbox.io/s/vq6k6195rl

    Explanation:

    For a select, you can define your selects like this:

    const myFancyQuestions = [
      { id: 1, label: 'Why?', category: 'Asking why' },
      { id: 2, label: 'Who?', category: 'Asking who' },
      { id: 3, label: 'When?', category: 'Asking when' }
    ];
    

    Then wrap your component with Field component.

    
    

    And then just show it in your render:

    class ObjectSelect extends React.Component {
      render() {
        const { input, multiple, options, value, ...rest } = this.props;
        const parse = event => {
          // This is what redux-form will hold:
          return JSON.parse(event.target.value);
        }
        return (
          
        )
      }
    }
    

提交回复
热议问题