在我的表单中使用反应评级组件时如何设置值的状态?
How to set state of the value when using react-rating component in my form?
我正在使用 React 评级 (https://github.com/dreyescat/react-rating)
而且我不确定如何为我的 2 个评分设置值的状态。其他领域没问题。
我能够获取要在控制台中显示的值。我不知道如何在 handleRatingChange() 中设置状态。
此外,当我完成评分后填写其他字段时,评分会重置。
这是我的代码:
var Rating = require('react-rating')
class Assignment extends Component {
constructor(props) {
super(props)
this.state = {
form: {
site: '',
facilityRate: '',
staffRate: '',
theList: 'off'
}
}
}
handleChange(event) {
const formState = Object.assign({}, this.state.form);
formState[event.target.name] = event.target.value;
this.setState({ form: formState });
console.log(formState);
}
handleRatingChange(rating) {
console.log('rating', rating);
}
render() {
return (
<Grid>
<PageHeader id='header'>
Track your assignment <small className='subtitle'>Let us make a record of your assignment.</small>
</PageHeader>
<form id='assignment-form'>
<h3>Record your assignment</h3>
<Row>
<Col xs={5} md={5}>
<FormGroup>
<ControlLabel id='site'>Site Name</ControlLabel>
<FormControl type='text' name='site'
onChange={this.handleChange.bind(this)}
value={this.state.form.site}
/>
</FormGroup>
<FormGroup>
<ControlLabel id='facilityRate'>Rate the Facility</ControlLabel><br />
<Rating
name='facilityRate'
emptySymbol={<img src='../star-empty.png' className='icon' alt='empty star' />}
fullSymbol={<img src='../star-full.png' className='icon' alt='filled star' />}
onChange={this.handleRatingChange} />
</FormGroup>
<FormGroup>
<ControlLabel id='staffRate'>Rate the Staff</ControlLabel><br />
<Rating
name='staffRate'
emptySymbol={<img src='../star-empty.png' className='icon' alt='empty star' />}
fullSymbol={<img src='../star-full.png' className='icon' alt='filled star' />}
onChange={this.handleRatingChange} />
</FormGroup>
<FormGroup>
<ControlLabel id='theList'>The List
<br /><i>Add it to my favorites.</i>
</ControlLabel><br />
<Checkbox
name='theList'
checked={this.state.theList}
onChange={this.handleChange.bind(this)}>Add to The List
</Checkbox>
</FormGroup>
</Col>
</Row>
<Row>
<Col xs={5} md={5}>
<Button type='submit' className='btn btn-secondary' id='submit'>Submit Assignment</Button>
</Col>
</Row>
</form>
</Grid>
);
}
}
添加初始评级 属性,例如 initialRating={this.state.facilityRating} 用于 facilityRate 评级,然后 initialRating={this.state.staffRating} 用于 staffRate 评级。
<Rating name='facilityRate'
emptySymbol={<img src='../star-empty.png'
className='icon' alt='empty star' />}
initialRating={this.state.form.facilityRate}
fullSymbol={<img src='../star-full.png' className='icon'
alt='filled star' />}
onChange={this.handleStaffRatingChange} />
<Rating name='staffRate'
emptySymbol={<img src='../star-empty.png'
className='icon' alt='empty star' />}
initialRating={this.state.form.staffRate}
fullSymbol={<img src='../star-full.png' className='icon'
alt='filled star' />}
onChange={this.handleStaffRatingChange} />
在你的状态下有一个默认值也很重要,因为一开始它是未定义的。
constructor(props) {
super(props)
this.state = {
form: {
site: '',
facilityRate: '', <-- Sets the initial value for facility
staffRate: '', <-- Sets the initial value for staff
theList: 'off'
},
}
}
然后将您的评级函数拆分为两个函数。
handleStaffRatingChange = (rating) => {
this.setState({...this.state, form: {...this.state.form, staffRate: rating}});
}
handleFacilityRatingChange = (rating) => {
this.setState({...this.state, form: {...this.state.form, facilityRate: rating}});
}
我们这样做的原因是每个函数都会改变状态中的特定区域。每个评级都必须查看状态以确定其值是什么,因此它们在状态中的值不能相同。 handleStaffRatingChange 目标 this.state.form.staffRate 和 handleFacilityRatingChange 目标 this.state.form.facilityRate.
可以设计一个函数来处理这两个问题,但由于您对反应还很陌生,所以我不想一次向您抛出太多东西。
这些函数正在添加一个扩展运算符来复制状态,然后使用 'rating' 的新值更新键 'facilityRating' 或 'staffRating'。我也在这里做一个 lambda 来处理绑定。
您的表单似乎正在重置,因为您正在替换状态而不是添加状态。再次使用展开运算符。
handleChange(event) {
const formState = Object.assign({}, this.state.form);
formState[event.target.name] = event.target.value;
this.setState({ ...this.state, form: formState });
console.log(formState);
}
我正在使用 React 评级 (https://github.com/dreyescat/react-rating) 而且我不确定如何为我的 2 个评分设置值的状态。其他领域没问题。
我能够获取要在控制台中显示的值。我不知道如何在 handleRatingChange() 中设置状态。
此外,当我完成评分后填写其他字段时,评分会重置。
这是我的代码:
var Rating = require('react-rating')
class Assignment extends Component {
constructor(props) {
super(props)
this.state = {
form: {
site: '',
facilityRate: '',
staffRate: '',
theList: 'off'
}
}
}
handleChange(event) {
const formState = Object.assign({}, this.state.form);
formState[event.target.name] = event.target.value;
this.setState({ form: formState });
console.log(formState);
}
handleRatingChange(rating) {
console.log('rating', rating);
}
render() {
return (
<Grid>
<PageHeader id='header'>
Track your assignment <small className='subtitle'>Let us make a record of your assignment.</small>
</PageHeader>
<form id='assignment-form'>
<h3>Record your assignment</h3>
<Row>
<Col xs={5} md={5}>
<FormGroup>
<ControlLabel id='site'>Site Name</ControlLabel>
<FormControl type='text' name='site'
onChange={this.handleChange.bind(this)}
value={this.state.form.site}
/>
</FormGroup>
<FormGroup>
<ControlLabel id='facilityRate'>Rate the Facility</ControlLabel><br />
<Rating
name='facilityRate'
emptySymbol={<img src='../star-empty.png' className='icon' alt='empty star' />}
fullSymbol={<img src='../star-full.png' className='icon' alt='filled star' />}
onChange={this.handleRatingChange} />
</FormGroup>
<FormGroup>
<ControlLabel id='staffRate'>Rate the Staff</ControlLabel><br />
<Rating
name='staffRate'
emptySymbol={<img src='../star-empty.png' className='icon' alt='empty star' />}
fullSymbol={<img src='../star-full.png' className='icon' alt='filled star' />}
onChange={this.handleRatingChange} />
</FormGroup>
<FormGroup>
<ControlLabel id='theList'>The List
<br /><i>Add it to my favorites.</i>
</ControlLabel><br />
<Checkbox
name='theList'
checked={this.state.theList}
onChange={this.handleChange.bind(this)}>Add to The List
</Checkbox>
</FormGroup>
</Col>
</Row>
<Row>
<Col xs={5} md={5}>
<Button type='submit' className='btn btn-secondary' id='submit'>Submit Assignment</Button>
</Col>
</Row>
</form>
</Grid>
);
}
}
添加初始评级 属性,例如 initialRating={this.state.facilityRating} 用于 facilityRate 评级,然后 initialRating={this.state.staffRating} 用于 staffRate 评级。
<Rating name='facilityRate'
emptySymbol={<img src='../star-empty.png'
className='icon' alt='empty star' />}
initialRating={this.state.form.facilityRate}
fullSymbol={<img src='../star-full.png' className='icon'
alt='filled star' />}
onChange={this.handleStaffRatingChange} />
<Rating name='staffRate'
emptySymbol={<img src='../star-empty.png'
className='icon' alt='empty star' />}
initialRating={this.state.form.staffRate}
fullSymbol={<img src='../star-full.png' className='icon'
alt='filled star' />}
onChange={this.handleStaffRatingChange} />
在你的状态下有一个默认值也很重要,因为一开始它是未定义的。
constructor(props) {
super(props)
this.state = {
form: {
site: '',
facilityRate: '', <-- Sets the initial value for facility
staffRate: '', <-- Sets the initial value for staff
theList: 'off'
},
}
}
然后将您的评级函数拆分为两个函数。
handleStaffRatingChange = (rating) => {
this.setState({...this.state, form: {...this.state.form, staffRate: rating}});
}
handleFacilityRatingChange = (rating) => {
this.setState({...this.state, form: {...this.state.form, facilityRate: rating}});
}
我们这样做的原因是每个函数都会改变状态中的特定区域。每个评级都必须查看状态以确定其值是什么,因此它们在状态中的值不能相同。 handleStaffRatingChange 目标 this.state.form.staffRate 和 handleFacilityRatingChange 目标 this.state.form.facilityRate.
可以设计一个函数来处理这两个问题,但由于您对反应还很陌生,所以我不想一次向您抛出太多东西。
这些函数正在添加一个扩展运算符来复制状态,然后使用 'rating' 的新值更新键 'facilityRating' 或 'staffRating'。我也在这里做一个 lambda 来处理绑定。
您的表单似乎正在重置,因为您正在替换状态而不是添加状态。再次使用展开运算符。
handleChange(event) {
const formState = Object.assign({}, this.state.form);
formState[event.target.name] = event.target.value;
this.setState({ ...this.state, form: formState });
console.log(formState);
}