【问题标题】:RadioGroup not passing the accurate value to the parent stateRadioGroup 没有将准确的值传递给父状态
【发布时间】:2020-12-26 12:02:14
【问题描述】:

我有一个带有单选按钮的单选组,这些单选按钮最初是未选中的,但是当我选中它们时,状态的值是选中按钮的值的一个。我正在使用 Material UI

这里是发生了什么的示例

const initialValues = {
    firstName: '',
    lastName: '',
    gender: ''
};
const [state, setState] = useState(initialValues);
const handler = (e) => {
    setState({
        ...state, [e.target.name]: e.target.value
    });
}
return (
    <ChildComponent handler={handler} {...state} />
)

那么,在&lt;ChildComponent /&gt;

const { fristName, lastName, gender, handler } = props;
return (
    <FormControl component='fieldset'>
        <RadioGroup onChange={handler} name='gender' value={gender}>
            <FormControlLabel value='feminine' control={<Radio />} label='Feminine' />
            <FormControlLabel value='masculine' control={<Radio />} label='Masculine />
            <FromControlLabel value='others' control={<Radio />} label='Others />
        </RadioGroup>
    </FormControl>
)

&lt;ChildComponent /&gt; 的 gender 状态的本地副本在登录控制台时是正确的,但是,父状态在更新时似乎会延迟。

检查feminine 值时,父状态保持为空,但子状态会更新。当检查masculine 值时,父状态更新为feminine,但子状态正确更新为masculine。

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    我测试了你的代码! 首先它有很多语法错误:

    • 您忘记在标签 'Masculine' 和 'Others' 后关闭引号
    • 您打错了第三个“FormControlLabel”,并且输入了“From”而不是“Form”

    除了这些语法错误之外,您的代码运行良好,并且一旦我更改性别,父母的状态就会正确更新。

    父.js

    import React , {useState} from 'react';
    import ChildComponent from './Child';
    
    const Parent = () => {
      const initialValues = {
        firstName: '',
        lastName: '',
        gender: ''
      };
      const [state, setState] = useState(initialValues);
      console.log("Parent's state", state);
      const handler = (e) => {
        setState({
          ...state, [e.target.name]: e.target.value
        });
      }
      return (
        <ChildComponent handler={handler} {...state} />
      )
    }
    
    export default Parent;
    

    Child.js

    import React from 'react'
    import {FormControl,RadioGroup, FormControlLabel, Radio} from '@material-ui/core';
    
    const Child = (props) => {
      const { fristName, lastName, gender, handler } = props;
      console.log('child's props', props);
      return (
        <FormControl component='fieldset'>
            <RadioGroup onChange={handler} name='gender' value={gender}>
                <FormControlLabel value='feminine' control={<Radio />} label='Feminine' />
                <FormControlLabel value='masculine' control={<Radio />} label='Masculine' />
                <FormControlLabel value='others' control={<Radio />} label='Others' />
            </RadioGroup>
        </FormControl>
      )
    }
    
    export default Child;
    

    【讨论】:

    • hmm..是不是因为我把console.log放错了地方?我把它放在处理函数中。
    • 就是这样。状态不会立即改变,但可以在下一个渲染周期中观察到它的变化。
    猜你喜欢
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    相关资源
    最近更新 更多