发布开关输入布尔值(反应/打字稿)

时间:2019-01-14 01:00:34

标签: java reactjs typescript toggle ecmascript-7

我正在尝试发布开关输入切换控件的状态值,但是当我使用按钮发送发布请求功能submitRecommendation()时,出现了JSON parse error: Cannot deserialize instance of `boolean` out of START_OBJECT token; nested exception错误。

我在这里做错了什么?并有一种方法可以编写此函数以具有多个切换控件,而无需重复代码?

interface IState {
      mentorInfoComplete?: boolean;
      hasChanged?: boolean;
    }
class Component extends React.Component<Props, State> {
  public state: State = {
    mentorInfoComplete: false
  }


  public render() {
    const {} = this.props;
    const { mentorInfoComplete } = this.state;
    const {} = this;

  <div className={classes.switchContainer}>
                    <FormGroup row>
                        <FormControlLabel
                          control={
                            <Switch
                              checked={mentorInfoComplete}
                              onChange={this.handleChange}
                              value={mentorInfoComplete}
                            >Toggle</Switch>
                            }label="YES"
                            />
                      <Typography color="secondary" variant="body1" className={classes.toggleQuestions}>Is this question complete</Typography>
                    </FormGroup>
                  </div>

            <Button
              color="primary"
              className="reviewApplication-back"
              variant="contained"
              type="submit"
              onClick={this.submitRecommendation}>
              Recommend Approval
            </Button>

  private handleChange() {
     this.setState({
      mentorInfoComplete: true
   });
   console.log(this.state);
  }

private async submitRecommendation() {
  const { 
    application,
    mentorInfoComplete
   } = this.state;

    if (!application) {
        return;
      }

      try {
        await axios.post(
          `/mentorApplication/${match.params.applicationId}/checklist`,
          {
            mentorInfoComplete: { mentorInfoComplete }
                  }
            );
    }
    }

1 个答案:

答案 0 :(得分:1)

在不知道API的情况下,我的猜测是这是令人反感的行: mentorInfoComplete: { mentorInfoComplete }

您基本上就是要做到这一点 mentorInfoComplete: { true } 当我猜你想要是什么 mentorInfoComplete: true

尝试摆脱多余的括号,看看是否有帮助?