Const 组件无法读取 属性 of undefined 即使它是一个 prop?

Const component cannot read property of undefined even though it is a prop?

我有一个parent? class 包含一个随机问题和取决于问题类型的不同类型的用户输入。

  1. 3 个可单击的按钮,单击时会改变颜色
  2. 一个输入框
class Parent extends Component {
   state = {
   buttonColors: ["white", "white", "white"],
   questionType: 0,
   avg: 0,
   numbers: [],
   answer: "",
   number: 0,
   showAns: false,
   current: "",
   etc....
   }

   // This part basically handles clicking of 3 buttons
   clickedAnswer(userAnswer) {
     // 0 is (>)   //   1 is (=)        1 is (<)
     let colors = this.state.buttonColors;
     let guess = this.state.current;
     if (!guess.includes(1)) {
     //Hasn't Guessed Yet -Action- Select Clicked button
     //Change color of the Guessed button
     colors[userAnswer] = "#d0f0c0";
     guess[userAnswer] = 1;
     } else {
     //Clicked Previous Answer -Action- Unselect Clicked button
     colors = ["white", "white", "white"]; //Reset Colors
     colors[userAnswer] = "#d0f0c0";
     guess = [0, 0, 0]; // Reset Guess
     guess[userAnswer] = 1;
    }

    this.setState({
    current: guess,
    buttonColors: colors,
    });
  }
   
   render() {
      return (
         <RenderInput
                questionType={this.state.questionType}
                answer={this.state.answer}
                buttonColors={this.state.buttonColors}
                clickedAnswer={this.clickedAnswer}
                handleChange={this.handleChange}
                current={this.state.current}
                showAns={this.state.showAns}
                />
      )
   }

然后我在文件底部有一个 const Render,它根据问题类型动态呈现输入类型。

const RenderInput = (props) => {
    switch (props.questionType){
        case 0: case 1: case 2: case 3:
            return (
                <Row style={{ paddingBottom: "50px", paddingTop: "50px" }}>
                  <Col style={{height: 100, textAlign: "right"}}>
                    <button
                        id="btn1"
                        onClick={() => props.clickedAnswer(0)}
                        style={{
                        width: "200px",
                        height: "80px",
                        backgroundColor:
                            props.showAns && props.answer[0] === 1
                            ? "red"
                            : props.buttonColors[0],
                        }}
                    >
                        <span
                        style={{ fontFamily: "courier", fontSize: "35px" }}
                        >
                            {"Higher"}
                        </span>
                    </button>
                  </Col>

                  <Col style={{ height: 100, textAlign: "center" }}>
                    <button
                    id="btn2"
                    onClick={() => props.clickedAnswer(1)}
                    style={{
                        width: "250px",
                        height: "80px",
                        backgroundColor:
                        props.showAns && props.answer[1] === 1
                            ? "red"
                            : props.buttonColors[1],
                    }}
                    >
                    <span
                        style={{ fontFamily: "courier", fontSize: "35px" }}
                    >
                        {"Unchanged"}
                    </span>
                    </button>
                  </Col>

                  <Col style={{height: 100, textAlign: "left"}}>
                    <button
                        id="btn3"
                        onClick={() => props.clickedAnswer(2)}
                        style={{
                          width: "200px",
                          height: "80px",
                          backgroundColor:
                            props.showAns && props.answer[2] === 1
                              ? "red"
                              : props.buttonColors[2],
                        }}
                    >
                        <span
                          style={{ fontFamily: "courier", fontSize: "35px" }}
                        >
                          {"Lower"}
                        </span>
                    </button>
                  </Col>
                </Row>
            )
        case 4: case 5:
            return (
                <p
                style={{
                  fontSize: 25,
                  textAlign: "center",
                  fontFamily: "courier",
                  fontWeight: "bold",
                  paddingTop: "10px",
                }}
                >
                    <input
                        type="text"
                        value={props.current}
                        onChange={props.handleChange}
                        id="input1"
                        autoComplete="off"
                        maxLength="8"
                        style={{
                        color: !props.showAns ? "black" : "red",
                        fontSize: 55,
                        fontFamily: "courier",
                        fontWeight: "bold",
                        width: "280px",
                        height: "85px",
                        textAlign: "center",
                        }}
                    />
                </p>
            )
    }
}

然而,当我尝试点击按钮更改颜色时,我收到一条错误消息 cannot read property 'buttonColor' of undefined

我该如何解决这个问题?我基本上需要child?或 const 组件到 return 一个动作 user clicked button 1/2/3 到方法 clickedAnswer,它根据按钮 # 更改按钮颜色,然后将更新后的配色方案发送回 const 和 re-render 反映颜色更新的输入部分。

当您调用 clickedAnswer={this.clickedAnswer} 时,方法中的 this 引用为空。将其替换为 clickedAnswer={this.clickedAnswer.bind(this)} 以绑定到当前对象,以便它可以访问状态。

您可以在此处阅读有关 bind 方法的信息:Function.prototype.bind()