0

Im try to display what user input below edit text but its not working all text fields link together

How can I make it work I need to display what user enter in below Label

class ComposedTextField extends React.Component {
  state = {
    name: '',
    title: '',
    email: '',
    experience: ''
  };

  handleChange = event => {
    this.setState({ 
          name: event.target.value, 
          title: event.target.value,
          email: event.target.value,
          experience: event.target.value
        });
  };

  render() {
    const { classes } = this.props;

    return (
      <div className={classes.container}>
        <Typography variant="headline">Header Info</Typography>
        <FormControl className={classes.formControl}>
          <InputLabel htmlFor="name-simple">Name</InputLabel>
          <Input id="name-simple" value={this.state.name} onChange={this.handleChange}/>
          <p>{this.state.name}</p>
        </FormControl>
        <FormControl className={classes.formControl}>
          <InputLabel htmlFor="title-simple">Title</InputLabel>
          <Input id="title-simple" value={this.state.title} onChange={this.handleChange}/>
          <p>{this.state.title}</p>
        </FormControl>
        <FormControl className={classes.formControl}>
          <InputLabel htmlFor="email-simple">Email</InputLabel>
          <Input id="email-simple" value={this.state.email} onChange={this.handleChange}/> 
          <p>{this.state.email}</p>
        </FormControl>
        <FormControl className={classes.formControl}>
          <InputLabel htmlFor="experience-simple">Experience</InputLabel>
          <Input id="experience-simple" value={this.state.experience} onChange={this.handleChange}/>
          <p>{this.state.experience}</p>
        </FormControl>
      </div>
    );
  }
}

How can I make this work please help me Im newbie

HemalHerath
  • 1,006
  • 2
  • 18
  • 38

2 Answers2

1

Change handleChange method to this

 handleChange = event => {
    this.setState({ [event.target.name]:event.target.value });
  };

And decorate your input text elements with name attribute i.e.

<Input name="name" id="name-simple" value={this.state.name} onChange={this.handleChange}/>

<Input name="email" id="email-simple" value={this.state.email} onChange={this.handleChange}/> 

Similarly you can do it for rest of the elements.

Now when you input something in the textboxes it will be set in state with current target's name and value

Manoz
  • 6,507
  • 13
  • 68
  • 114
0

Use your handle change method like as follows. This will fix yours

handleChange = event => {
  const { name, value } = event.target
  this.setState({ [name]: value });
};
Jeeva
  • 1,550
  • 12
  • 15