【问题标题】:Update state in Child更新 Child 中的状态
【发布时间】:2021-01-17 13:53:18
【问题描述】:

我很困惑。我试用了本教程https://material-ui.com/components/radio-buttons/#radiogroup。但是每次我点击一个单选按钮时它都没有填满。

如果要设置状态,我检查了 handleFunctions。是的,它正确设置了新状态。所以我不知道我的代码哪里不对。

感谢所有回答

这是我的代码:

https://codepen.io/Y4roc/pen/MWjLWqz

class Registration extends Component {

    constructor(props) {
        super(props);
        this.handlePackage = this.handlePackage.bind(this);
        this.nextStep = this.nextStep.bind(this);
        this.state = {
            form: {
                package: 0
            },
            step: 0
        };
    }

    handlePackage(event) {
        this.setState({form: {package: event.target.value}})
    }

    nextStep() {
        this.setState((state) => {
            return {step: state.step++};
        })
    }

    render() {
        const steps = [
            'Package',
            'Account',
            'Invoice Address',
            'Frist Steps'
        ];


        return <AuthenticationForm lg={6} md={8} sm={10}><Grid item>
            <Typography component="h1" variant="h5" gutterBottom>
                Registration
            </Typography>
        </Grid>
            <Grid item>
                <form>
                    <Stepper alternativeLabel activeStep={this.state.step}>
                        {steps.map((label) => (
                            <Step key={label}>
                                <StepLabel>{label}</StepLabel>
                            </Step>
                        ))}
                    </Stepper>
                    {(this.state.step == 0) ? (<Grid container direction={"column"} spacing={2}>
                        <Grid item>
                            <FormControl component="fieldset">
                                <FormLabel component="legend">Choose your package</FormLabel>
                                <RadioGroup name={"package"} value={this.state.form.package} onChange={this.handlePackage}>
                                    <FormControlLabel value={1} control={<Radio/>} label={"Demo"}/>
                                    <FormControlLabel value={2} control={<Radio/>} label={"Bronze"} disabled/>
                                    <FormControlLabel value={3} control={<Radio/>} label={"Silber"} disabled/>
                                    <FormControlLabel value={4} control={<Radio/>} label={"Gold"} disabled/>
                                </RadioGroup>
                            </FormControl>
                        </Grid>
                        <Grid item>
                            <Button variant={"contained"} color={"primary"} onClick={this.nextStep}>
                                Next
                            </Button>
                        </Grid>
                    </Grid>) : null}
                </form>
            </Grid>
        </AuthenticationForm>;
    }
}

AuthenticationForm 元素是为我的应用程序的这一部分设计对话框的包装器。

class AuthenticationForm extends Component {
    render() {
        const {classes, lg, md, sm, children} = this.props

        return <Grid container spacing={0} justify="center" alignItems="center" direction="row"
                     className={classes.root}>
            <Grid item lg={lg} md={md} sm={sm}>
                <Grid container direction="column" justify="center" spacing={2}>
                    <Paper variant={"elevation"} className={classes.paper}>
                        {children}
                    </Paper>
                </Grid>
            </Grid>
        </Grid>;
    }
}

【问题讨论】:

  • 你为什么使用this.forceUpdate() ??更新状态时已经发生了渲染!
  • 这是一个测试。在复制代码之前,我忘记删除它。
  • 你能在代码沙箱里做这个吗?我帮你解决问题。
  • 我添加了code pen的链接(codepen.io/Y4roc/pen/MWjLWqz)

标签: reactjs material-ui


【解决方案1】:

您的新状态对象与初始状态不匹配,它缺少 package 属性。

onClick 事件返回字符串类型的value

const { FormControl, RadioGroup, FormControlLabel, Radio } = MaterialUI

class Registration extends React.Component {
  state = { form: { package: '1' }, step: 0 };

  handlePackage = (event) => {
    this.setState({ form: { package: event.target.value } });
  };

  render() {
    return (
      <FormControl component="fieldset">
        <RadioGroup
          name={"package"}
          value={this.state.form.package}
          onChange={this.handlePackage}
        >
          <FormControlLabel value={"1"} control={<Radio />} label={"Demo"} />
          <FormControlLabel value={"2"} control={<Radio />} label={"Bronze"} />
          <FormControlLabel value={"3"} control={<Radio />} label={"Silber"} />
          <FormControlLabel value={"4"} control={<Radio />} label={"Gold"} />
        </RadioGroup>
      </FormControl>
    );
  }
}

ReactDOM.render(<Registration />, document.getElementById('root'))
<script src="https://unpkg.com/react@latest/umd/react.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/react-dom@latest/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@material-ui/core@latest/umd/material-ui.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/babel-standalone@latest/babel.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
<div id="root"></div>

【讨论】:

  • 我在 codepen 中更新了我的代码。它不起作用。可以看看吗?
  • 点击“运行代码 sn-p”。它确实有效。
  • 只需将初始状态字符串设为state = { form: { package: '1' }, step: 0 };
  • 是的,但请看我在代码笔中的示例。这没有用。我用你的解决方案更新了它。
  • 同时更新 FormControlLabelvalue 属性。或者,您可以在 onClick 处理程序中转换为数字类型。
【解决方案2】:

只需将handlePackage 更改为:

handlePackage(event) {
    this.setState({form: { package: Number(event.target.value)}});
}

电台组对此:

<RadioGroup name="package" value={this.state.form.package} onChange={this.handlePackage}>

【讨论】:

    【解决方案3】:

    我想改一下

            this.state = {
                form: {
                    package: 0
                },
                step: 0
            };
    

            this.state = {
                form: 1,
                step: 0
            };
    

    可能会起作用。

    【讨论】:

    • 抱歉,这不是解决方案。
    • @thhan 这确实是一个解决方案。在你的问题中,值是this.state.form,所以我认为包没用。如果您根据我编辑您的代码,它将起作用。您没有说您希望值为this.state.form.package
    • 问题是,事件返回的是字符串而不是数字。
    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 2021-06-25
    • 2021-09-25
    • 2018-08-05
    • 2020-09-30
    • 1970-01-01
    • 2017-04-06
    • 2023-01-28
    相关资源
    最近更新 更多