My text filed is not changing

import React, {Component} from "react"

class App extends Component {
    constructor() {
        this.state = {
            firstName: "",
            lastName: "",
            age: "",
            gender: "",
            destination: "",
            dietary: false,
            dietaryrestriction: ""
        let style = false
        this.handleChange = this.handleChange.bind(this)
        const {name,type,value,checked} = = this.state.dietary ? {display : 'block'} : {display: 'none'}
        this.setState(type === "checkbox" ? {[name]: checked} : {[name]: value})
    render() {
        return (
                    <input type="text" name="firstName" value={this.state.firstName} placeholder="First Name" onClick={this.handleChange} />
                    <br />
                    <input type="text" name="lastName" value={this.state.lastName} placeholder="Last Name" onClick={this.handleChange} />
                    <br />
                    <input type="number" name="age" value={this.state.age} placeholder="Age" onClick={this.handleChange} />
                    <br />
                    {/* Create radio buttons for gender here */}
                    <br />
                    <input type="radio" name="gender" value="male" checked={this.state.gender === "male"} onChange={this.handleChange} />
                    <br />
                    <input type="radio" name="gender" value="female" checked={this.state.gender === "female"} onChange={this.handleChange} />
                    <br />
                    {/* Create select box for location here */}
                    <br />
                    <select value={this.state.destination}>
                        <option value="India" onChange={this.handleChange}>India</option>
                        <option value="Japan" onChange={this.handleChange}>Japan</option>
                        <option value="Bhutan" onChange={this.handleChange}>Bhutan</option>
                    {/* Create check boxes for dietary restrictions here */}
                    <br />
                    <br />
                    <input type="radio" value={this.state.dietary} onChange={this.handleChange} checked={this.state.dietary} />
                    Is there any dietary restrection?
                    <textarea style={} value={this.state.dietaryrestriction} onChange={this.handleChange} />
                    <br />
                    <br />
                <hr />
                <h2>Entered information:</h2>
                <p>Your name: {this.firstName}{this.lastName}</p>
                <p>Your age: {this.age}</p>
                <p>Your gender: {this.gender}</p>
                <p>Your destination: {this.destination}</p>
                <p style={}>
                    Your dietary restrictions: 
                    {/* Dietary restrictions here, comma separated */}

export default App

the text field,number and other n=inputs are not responding.
I have binded the handleChange(), then too it is not working.
Appreciate any help.

You need to create make sure your onChange event handler also considers the text fields, so that everytime there is a change into it, you modify the state, if not by binding them, you will not be able to use them:

Add onChange={this.handleChange}

<input type="text" name="firstName" value={this.state.firstName} placeholder="First Name" onClick={this.handleChange} onChange={this.handleChange} />

Also the other things you are going to have to do is modify your change handler to not only consider checkbox inputs, but also text fields, would normally recommend something like this:

  onChange = e => {

Hope that helps!

1 Like