简体   繁体   English

React on Rails:无法在我的表单输入框中输入文本

[英]React on Rails: Can't enter text in my form input boxes

When I try to type in the boxes on the webpage it doesn't register that I am typing anything.当我尝试在网页上的框中输入内容时,它不会记录我正在输入任何内容。 I am guessing it has something to do with the handleChange or onChange, but I could use some help here.我猜它与 handleChange 或 onChange 有关,但我可以在这里使用一些帮助。 I am still pretty new to React and trying to figure it out.我对 React 还是很陌生,并试图弄清楚。 What am I missing here?我在这里想念什么?

import React, {Component} from 'react';
import { Form } from 'semantic-ui-react';

class Assessments extends Component {
  state = {assessment_name: '', assessment_type: ''}

  componentDidMount() {
    if (this.props.id) {
      const { assessment_name, assessment_type } = this.props
      this.setState({ assessment_name, assessment_type })
    }
  }

  handleChange = (a) => {
    const { name, value } = a.target
    this.setState({ [name]: value })
  }

  handleSubmit = (a) => {
    a.preventDefault()
    if (this.props.id) {
      const { id, history } = this.props
      this.props.updateName(id, this.state, history)
      this.props.toggleUpdate()
    }
    this.props.close()
    this.setState({ assessment_name: '', assessment_type: ''})
    }

    close = () => this.setState({ open: false })

    render() {
      const { assessment_name, assessment_type } = this.state
      return(
        <Form onSubmit={this.handleSubmit}>
          <Form.Input
          name=''
          value={assessment_name}
          onChange={this.handleChange}
          label='Assessment Name'
          required
          />
          <Form.Input
          name='AssessmentType'
          value={assessment_type}
          onChange={this.handleChange}
          label='Assessment Type'
          required
          />

          
          <Form.Button>Submit</Form.Button>
        </Form>
      )
    }
  }

export default Assessments;

You're not passing the right name s to the Form.Input components which the handleChange function uses to update the state.您没有将正确的name传递给handleChange function 用于更新 state 的Form.Input组件。 They have to be 'assessment_name' and 'assessment_type' respectively to make sure the state gets updated on input change events and the new values get reflected on the fields.它们必须分别是'assessment_name''assessment_type' ,以确保 state 在输入更改事件时得到更新,并且新值会反映在字段上。

<>
  <Form.Input
    name="assessment_name"
    value={assessment_name}
    onChange={this.handleChange}
    label="Assessment Name"
    required
  />
  <Form.Input
    name="assessment_type"
    value={assessment_type}
    onChange={this.handleChange}
    label="Assessment Type"
    required
  />
</>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM