I'm developing a simple opinion poll as my way of ramping up on reactjs. So far I've come up with the following:
//App.js
import React, { Component } from 'react';
import './App.css';
const PollOption = ({options}) => {
return (
<div className="pollOption">
{options.map((choice, index) => (
<label key={index}>
<input type="radio"
name="vote"
value={choice.value}
key={index}
defaultChecked={choice.value}
onChange={() => this.props.onChange()}/>
{choice.text}
</label>
))}
</div>
);
};
class OpinionPoll extends Component{
constructor(props) {
super(props);
this.state = {selectedOption: ''}
}
handleClick(){
console.log('button clicked');
}
handleOnChange(){
console.log('foo');
}
render(){
return (
<div className="poll">
{this.props.model.question}
<PollOption
options={this.props.model.choices}
onChange={() => this.handleOnChange()}/>
<button onClick={() => this.handleClick()}>Vote!</button>
</div>
);
}
}
export default OpinionPoll;
//index.js
var json = {
question: 'Do you support cookies in cakes?',
choices:
[
{text: "Yes", value: 1},
{text: "No", value: 2}
]
}
const root = document.getElementById("root");
render(<OpinionPoll model ={json} />, root)
I want to get the value of the radio button when the button is clicked.