2017-10-10 73 views
0

我正嘗試使用redux-form的FieldArrays來構建表單。 fields數組中的每個字段都需要有一個「訂單」字段。我想用默認/初始值預填充該字段。我知道如何向表格提供initialValues,但不知道如何爲個人Field做到這一點。如何向redux形式的單個字段提供初始值?

const renderQuestion = ({ fields, meta: { error, submitFailed } }) => (
    <ul> 

    {fields.map((question, index) => (
     <li key={index}> 
     <button type="button" title="Remove Question" onClick={() => fields.remove(index)}> 
      x 
     </button> 
     <h4>Question #{index + 1}</h4> 
     <Field 
      name={`${question}.order`} 
      value={index + 1} // This line is an example to what I am trying to achieve 
      type="number" 
      component={renderField} 
      label="Order" /> 
     <Field name={`${question}.prompt`} type="text" component={renderField} label="Prompt" />   
     </li> 
    ))} 
    </ul> 
); 

const QuizStepAddForm = props => { 
     ... 
     <FieldArray name="questions" component={renderQuestion} /> 
     ... 
}; 

回答

0
+0

別開玩笑了:)。謝謝! – SaidAkh

+0

再次感謝@kngroo。你能再幫我一次嗎?你能告訴我如何將這個'initial'值正確地提供給包裝組件 - 'renderField',這樣這個初始值將成爲用戶可以改變的起點。我嘗試過這樣做:' SaidAkh

+0

我不完全確定你的意思。你想把一些初始值作爲一個道具傳遞給一個包裝''的包裝組件?您可能想要閱讀有關受控和不受控制的組件: https://reactjs.org/docs/forms.html#controlled-components – kngroo