2017-02-21 1 views
0

어떤 이유로 Field 값이 내 customComponent const에 인쇄되지 않습니다.필드 값이 사용자 지정 구성 요소에 인쇄되지 않음

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의 전체 아이디어는 라이브러리가 알아서 할 수 있다는 것입니다.

당신이이

는 그냥 reduxForm 위의 REDUX connect -decorator 스틱 (워드 프로세서에 "Initialize from state" 예 참조) 수행 할 initialValues 프로 프를 사용합니다 REDUX 형태 Field의 초기 값을 설정하려면

- 데코레이터 및 redux 상태를 사용하여 초기 양식 값을 설정할 수 있습니다.

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 {} 

희망이 있습니다.

관련 문제