【问题标题】:How to set state of the value when using react-rating component in my form?在我的表单中使用 react-rating 组件时如何设置值的状态?
【发布时间】:2018-04-04 02:11:12
【问题描述】:

我正在使用 React Rating (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>
    );
  }
}

【问题讨论】:

    标签: reactjs setstate react-rating


    【解决方案1】:

    添加初始评级属性,例如为 facilityRate Rating 添加 initialRating={this.state.facilityRating},然后为 staffRate Rating 添加 initialRating={this.state.staffRating}。

    <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 的新手。非常感谢。
    • 太棒了,我昨晚测试了代码,它似乎在我的测试项目中成功了。如果您有任何问题,请告诉我。
    • 我试过了。评级仍然存在并且不会重置,这太棒了。我只是没有让我对 Facility 或 Staff 的评分进入表单状态。此外,在我对设施进行评级然后对员工进行评级后,设施将更改为员工的评级。 React 对我来说更加复杂,因为我正在使用这个其他组件 React-Rating。
    • 我最初为您的评级做了一个基本示例...评级同步变化的原因是因为两个评级都指向该州的相同值。抱歉,我在您的问题上忽略了这一点。现在我将函数一分为二,每个函数都将处理与其关联的商店中的值,并且您的信息将以表格形式存储在其所需位置。如果您开始查看这些额外的库并真正将它们区分开来,您会一遍又一遍地看到它几乎是相同的歌曲和舞蹈。这只是语法。
    • 再次感谢约翰(我不知道为什么我之前叫你 JD,对不起)的帮助!我稍后再试试,然后回来。有很多东西要向你学习。我从来没有做过传播运算符,所以我要读一下。以及您如何编写评级函数,我从未见过像您这样的箭头函数。我已经完成了箭头函数,但从未在函数名称和参数之间使用等号:handleStaffRatingChange = (rating)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2018-12-04
    • 1970-01-01
    • 2020-05-16
    • 2021-10-31
    • 2019-11-29
    • 2020-10-29
    相关资源
    最近更新 更多