【问题标题】:React app, after getting data from initial get request, onChange doesnt workReact应用程序,从初始获取请求获取数据后,onChange不起作用
【发布时间】:2020-11-14 09:24:33
【问题描述】:
 constructor (props) {
    super(props)
    this.state = {
      AyarAdi: '',
      Bilgi: '',
      FirmaKodu: '',
      icerik: {
        smsUser: '',
        smsApi: '',
        mesajAciklama: '',
        password: ''
      },
      errors: {}
    }
    this.handleChange = this.handleChange.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }


  componentDidMount () {
    this.props.AyarlarListesiAl(this.props.user.userCreds.FirmaKodu)
  }

  handleChange (event) {
    this.setState({
      icerik: {
        ...this.state.icerik,
        [event.target.name]: [event.target.value]
      }
    })
  }


 render(){
        <div>
          <InputGroup className='mb-3'>
            <FormControl
              name='password'
              placeholder='SMS Şifre'
              onChange={this.handleChange}
              value={
                this.props.data.AyarlarListesi[0] !== undefined
                  ? JSON.parse(this.props.data.AyarlarListesi[0].Bilgi).password
                  : this.state.icerik.password
              }
              style={{ maxWidth: 400, height: 40 }}
            />
            <span style={{ color: 'red' }}>
              {this.state.errors['password']}
            </span>
          </InputGroup>
        </div>
        }

const mapStateToProps = state => ({
  user: state.user,
  data: state.data
})

....

问题是,在componentDidMount中,我通过Redux action向服务器发送了一个get请求,它从redux reducer state中获取了我的数据; AyarlarListesi。我想将数据获取到输入元素值,因此当用户打开页面时,他/她可以更新以前的数据,只需在输入上更改它并单击提交。我可以成功地将数据放入输入,但我无法更改输入的值,它保持稳定,handleChange 函数不起作用。我怎样才能让它发挥作用?

【问题讨论】:

  • 如果你的 AyarlarListesi 数组在第 0 个索引上有元素,那么值是从 props 数据而不是 state 数据设置的,并且在 onChange 处理程序中,你正在更改状态数据。因此您的更改不会得到反映。
  • 如何将数据传递给组件状态?在componentDidmount中,它没有立即执行,在渲染中,我可以将它传递给变量但不能通过setState传递给状态

标签: reactjs dom input redux server


【解决方案1】:

您的 handleChange 函数只处理状态。但是,要显示的值最好来自 props,并且只有在 props 字段不可用时才会回退到 state 变量。

要在通过用户输入更改状态变量后显示它,您可以交换条件并设置它以便状态变量将被显示,除非它为空(并使用 props 变量作为后备),或者您可以改为更新获得新道具时的初始状态变量。

假设您从不从外部更改道具或不关心以后的更新,这将是我解决您问题的首选方式:

componentDidMount () {
  this.props.AyarlarListesiAl(this.props.user.userCreds.FirmaKodu);
  if (this.props.data.AyarlarListesi[0] !== undefined) {
    this.setState({
      icerik: {
        ...this.state.icerik,
        password: JSON.parse(this.props.data.AyarlarListesi[0].Bilgi).password,
      },
    });
  }
}

同样在您的 FormControl 中跳过检查是否设置了道具数据,而是始终使用 this.state.icerik.password 作为值。

如果您从外部获取的 props 可能发生变化,并且您希望在从外部传递新密码或 AyarlarListesi 时重新启动,则应再次更新 componentDidUpdate 中的状态:

componentDidUpdate (prevProps) {
  if (prevProps.data.AyarlarListesi !== props.data.AyarlarListesi && props.data.AyarlarListesi[0] !== undefined) {
    this.setState({
      icerik: {
        ...this.state.icerik,
        password: JSON.parse(this.props.data.AyarlarListesi[0].Bilgi).password,
      },
    });
  }
}

【讨论】:

  • 它不起作用,因为一开始props是未定义的,所以它永远不会进入条件并填充状态
  • 我已经更新了我的答案,向您展示如何更新道具更改的状态。注意:这不会检查您是否已经更改了状态,因此如果您的道具从外部更改,它将覆盖您在此期间对状态变量所做的任何更改
猜你喜欢
  • 1970-01-01
  • 2013-10-14
  • 2017-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多