2017-02-21 51 views
0

出于某种原因,字段值未打印到我的customComponent常量中。未在自定义组件中打印的字段值

const CustomComponent = function(field) { 
    console.log(field.input.value); //blank, no value 
    return (
    <div> 
     <input { ...field.input } type={field.type} placeholder={field.placeholder} className={field.className} /> 
    </div> 
); 
} 

...... 

<Field name="test" component={CustomComponent} type="text" placeholder="Test" value="testvalue" /> 
+0

你可以在输入中输入,但在日志中只能看到“undefined”?你的代码看起来很好。 –

回答

1

这里的问题是,你要直接设置redux-formField组件的value,但redux-form的整体思路是,你让图书馆采取照顾。

如果你想设置一个终极版形式Field的初始值,使用initialValues亚丙做到这一点(见文档"Initialize from state"例子)

只要坚持了Redux connect -decorator的reduxForm以上 - 装饰者,你可以使用REDX状态来设置初始表格值

class MyForm extends Component { 
    render() { 
    return (
     <form> 
     { /* This one will have the static initial value */ } 
     <Field 
      name="staticValue" 
      component={ CustomComponent } 
      type="input" 
      placeholder="Static Placeholder" 
     /> 
     { /* This one will have the dynamic initial value */ } 
     <Field 
      name="dynamicValue" 
      component={ CustomComponent } 
      type="input" 
      placeholder="Dynamic Placeholder" 
     /> 
     </form> 
    ) 
    } 
} 

const mapStateToProps = reducers => { 
    return { 
    initialValues: { 
     staticValue: 'some static default initial value', 
     dynamicValue: reducers.dynamicReducer.dynamicValue 
    } 
    } 
} 

@connect(mapStateToProps) 
@reduxForm({ formName: 'MyForm' }) 
class MyFormContainer extends Myform {} 

希望这有助于!