Last active
September 10, 2018 12:57
-
-
Save tarang9211/57b82a8d15eb50fb5656f5553c9b1981 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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