2017-12-27 23 views
1

이벤트시 특정 필드의 값을 어떻게 변경할 수 있습니까?redux-form : 클릭시 특정 필드 값을 변경하십시오.

작동하지 (재산 정의의 'FIRSTNAME'을 읽을 수 없습니다)

onclick1() { 
    this.props.fields.firstname.onChange("John") 
} 
render() { 
const { handleSubmit } = this.props; 
return (
    <div> 
    <form onSubmit={handleSubmit(this.submit.bind(this))}> 

     <Field 
     name="firstname" 
     component={TextField} 
     label="first name" 
     /> 
     <button onClick="this.onclick1()">Set name to John</button> 

     <button type="submit"> 
     okay 
     </button> 
    </form> 

이 솔루션은 여기에 제안했지만 나를 위해 https://stackoverflow.com/a/36916183/160059

REDUX 형태의 V7

작동하지 않습니다
+0

양식은 어떻게 보이나요? 몇 가지 코드를 공유합니까? – Dario

+0

@Dario 업데이트 된 예 – rendom

답변

1

코드에 몇 가지 문제점이 있습니다.

<button onClick="this.onclick1()"> 

은 다음과 같아야합니다

<button onClick={this.onclick1}> 

및 onclick1은 어떻게 든 구성 요소에 바인딩해야합니다. 게다가 필드 값을 설정하는 방법은 보통 change입니다. 그래서 내가 좋아하는 뭔가 코드를 변경합니다 : 난 단지 코드의 일부 관련 부분을 다시 썼다

class ComplexForm extends React.PureComponent { 
    constructor(props) { 
    super(props); 
    this.onclick1 = this.onclick1.bind(this); 
    } 

    onclick1() { 
    this.props.change("firstname", "John"); 
    } 

    render() { 
    const { handleSubmit } = this.props; 
    return (
     <form onSubmit={handleSubmit}> 
     <Field name="firstname" component="input" label="first name" /> 
     <button onClick={this.onclick1}>Set name to John</button> 
     </form> 
    ); 
    } 
} 

주와 나는 당신의 TextField이처럼 보이는 방법을 모르는 표준 input 구성 요소의 원인을 사용하고 있습니다. 온라인 데모 확인 here

+0

큰 감사드립니다. – rendom