简体   繁体   中英

React is not changing the state of the parent element

I'm building simple todo app in react and I have made input field as part of inputForm element which is child element.

I can pass functions as props from parent to child without problem, but I can't update parent state to store value on input field. When I type in input field, passed function is executing normally but currentTodo state is not updating.

I have found that this problem can be avoided by using single data flow pattern (like Flux or Reflux) but as this is my first project I want to understand how to work with basics.

Code for parent element:

import React, { Component } from 'react';
import './App.css';
import InputForm from '../components/InputForm'
import {Task} from '../components/Task'

class App extends Component {

    constructor(){
        super();
        this.state = {
            tasks: ["Todo", "Toda"],
            currentToDo: "",
        };
    }


//makes copy of task array, pushes current to do to copy and setsState 
//with new values
    addTodo = () => {
        console.log("addTodo")
        let copy = this.state.tasks.slice();
        console.log(this.state.currentToDo)
        copy.push(this.state.currentToDo);
        this.setState({tasks: copy});
    }

//gets input value from input field and updates current todo
    onInputChange = e => {
        console.log(e.target.value);
        this.setState({ currentTodo: e.target.value })
    }

  render() {
      let drawTask = this.state.tasks.map(e => {
          return <Task todo={e}/>
      })

    return (
        <div className="container">
            <InputForm onInputChange={() => this.onInputChange} add={this.addTodo}/>
            {drawTask}
        </div>
    );
  }
}

export default App;

Code for child element:

import React, { Component } from 'react';
import './component.css';
import {AddButton} from './Buttons.js'

class InputForm extends Component{
    constructor(){
        super();
        this.state = {

        }
    }

    render(){
        return(
            <div className='taskHeader'>
 {/*Value of current todo is send as props from parent element*/}
                <input value = {this.props.currentToDo} onChange={this.props.onInputChange()} type="text"/>
                <AddButton add = {this.props.add}/>
            </div>
        )
    }
}

export default InputForm;

You are calling the function during the render rather than passing a reference.

Parent owns the function and needs to pass it to the child:

<InputForm onInputChange={this.onInputChange} add={this.addTodo}/>

Now that the child has a prop called onInputChange, you pass it to the onChange callback as a reference.

<input value={this.props.currentToDo} onChange={this.props.onInputChange} type="text"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM