📅  最后修改于: 2022-03-11 14:53:20.350000             🧑  作者: Mango
const Row = function(props){
const {checked, value, onChange, onChecked} = props;
return (
);
}
class App extends React.Component {
constructor(props){
super(props);
this.state = {
rows: [
{value: 'row1', checked: false},
{value: 'row2', checked: true},
{value: 'row3', checked: false},
]
};
}
updateValue = (e, idx) => {
const rows = [...this.state.rows]; // copy array because we don't want to mutate the previous one
rows[idx].value = e.target.value;
this.setState({
rows,
});
}
onChecked = (idx) => {
const rows = [...this.state.rows]; // copy array because we don't want to mutate the previous one
rows[idx].checked = !rows[idx].checked;
this.setState({
rows,
});
}
addRow = () => {
const rows = [...this.state.rows,
{value:'', checked: false}
];
this.setState({
rows,
});
}
deleteRows = () => {
this.setState({
rows: this.state.rows.filter(e => !e.checked)
});
}
render(){
return(
{this.state.rows.map((row, idx) => {
return(
this.updateValue(e, idx)}
onChecked={() => this.onChecked(idx)}
/>
)
})
}
);
}
}
ReactDOM.render(
,
document.getElementById('app')
);