Editable Form ReactJS

前端 未结 2 1602
梦毁少年i
梦毁少年i 2020-12-07 05:17



        
相关标签:
2条回答
  • 2020-12-07 06:11

    First, the manner your are changing the state is not good. You are setting the new state to have only a single attribute which is the name and value of the field which is modified.

    Coming to how to print the value of the field you are modifying, you should do a console log in the handle change method.

    That said, your code should look like:

    handleInputChange(event) {
    
        //const {name, value} = event.target;
        //const oldState = this.state;
        //const newState = Object.assign({},oldState,{[name]:value});
        //this.setState(newState);
    
        this.setState({
            [event.target.name]: event.target.value
        });
    
        //printing the input name and value as it is being modified.
        console.log(name + ":" + value);
    
        //if you want to see the value of the current state uncomment the line below
        //console.log("State=" + JSON.stringify(newState));
    
    }
    
    printState = () => {
      console.log("State="+JSON.stringify(this.state));
    }
    

    For more insight on how to modify objects with assign method see: MDN doc on Object.assign()

    If you want to print the current state on clicking the save button then add the onClick attribute to the button as shown in the code below:

    <button value="Save" onClick={this.printState}/>
    
    0 讨论(0)
  • 2020-12-07 06:18

    Change InfoRow to

    class InfoRow extends Component {
    
        render() {
            return (
    
                <tr>
                    <td>
                      {this.props.beacon}
                    </td>
                    <td>
                    <input type="text"
                       className="form-control"
                       defaultValue={this.props.beaconValue}
                       value={this.props.name}
                       name={this.props.beaconValue}
                       onChange={(e) =>this.props.handleInputChangeProp(e.target.value)}
                    />
                    </td>
                </tr>
            )
        }
    }
    

    and render to

    Object.keys(this.props.ebcn).map((keyName, keyIndex) =>{
    
              return rows.push(<InfoRow beacon={keyName} beaconValue={a[keyName].toString()} name={this.state[keyName]} key={keyIndex} handleInputChangeProp={(inp) =>this.handleInputChange(keyName, inp)}/>);
           });
    

    and

    handleInputChange(beacon, value) {
    
            this.setState({
                [beacon]: value
            });
        }
    

    and handleClick to

     handleClick = () =>{
           Object.keys(this.props.ebcn).map((key)=> {
                console.log(this.state[key]);
           }))
      }
    
    0 讨论(0)
提交回复
热议问题