0

I am new to reactjs. What is the proper way to pass the parent value to child setState, I am having error says below, and I am unable to change input value, the value looks correct but unable to be changed.

"is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component"

export default class parent extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            title: ''
        }
    }

    updateFun(){
        this.setState({title: 'Parents title'});
    }

    render() {
        return (
            <div>
            <button onClick={() => this.updateFun()}> Update </button>
            <Child title = {this.state.title}/>
            </div>
        )
    }
}


export default class Child extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            title: ''
        }
    }

    render() {
        return (
            <div>
            <input 
                id="title" 
                type="text"
                value={this.props.title}
                />
            </div>
        )
    }
}
2
  • 2
    i think error is not in this part, this code looks proper, check working Fiddle there is one issue only, name of the parent component, it should be Parent check this answer for reason. Commented Aug 1, 2017 at 12:44
  • I agree with @MayankShukla. The code works fine as you posted it. It is, however, a bit unclear exactly what you want to do. Do you want the child to have its own state or should the Parent control all states? You have a title state in your Child that you never use. It's also unclear why you have a button that updates the input value with a static text. Commented Aug 1, 2017 at 12:45

1 Answer 1

6

You need to use defaultValue instead of value. Also you must add onChange handler to your input. To handle input changes.

Child example

export default class Child extends React.Component {
constructor (props) {
    super(props);
    this.state = {
        title: props.title
    }

    this.handler = this.handler.bind(this);
}

handler(event) {
    console.log(event.target.value);

    this.setState({title: event.target.value});
}

render() {
    return (
        <div>
        <input 
            id="title" 
            type="text"
            defaultValue={this.state.title}
            onChange={this.handler}
            />
        </div>
    )
}}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked nicely for me. Also noticed that the constructor block could be avoided. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.