Skip to content

Instantly share code, notes, and snippets.

@tarang9211
Last active September 10, 2018 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarang9211/57b82a8d15eb50fb5656f5553c9b1981 to your computer and use it in GitHub Desktop.
Save tarang9211/57b82a8d15eb50fb5656f5553c9b1981 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
class App extends Component {
state = {
questions: this.props.questions,
showState: false,
};
handleChange = id => e => {
const { questions } = this.state;
const selectedQuestion = questions.find(question => question.id === id);
selectedQuestion.value = e.target.value;
questions[id] = selectedQuestion;
this.setState({ questions });
};
handleSubmit = e => {
e.preventDefault();
this.setState({ showState: true });
};
render() {
return (
<div className='App'>
<form>
{questions.map(({ id, question, value }) => {
return (
<div key={id}>
<input
type="text"
name={question}
value={value}
placeholder={question}
onChange={this.handleChange(id)}
/>
</div>
);
})}
<button type="submit">Submit</button>
</form>
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment