我有两个非常简单的react组件,QF
和Child
,我试图将数据从json文件传递到Child
类,但是我总是得到'props' is not defined
错误,我觉得我遗漏了一些小东西,但我的React知识不够丰富,无法找到它。是否有其他方法可以将 prop 传递给子组件?先谢谢您!
我有点困惑,因为我一直看到不同的方法来设置React组件。
以下是父组件:
import React from "react";
import Child from "./Child";
import Records from "./CriterionQuestions.json";
class QF extends React.Component {
state = {
response: "",
};
handleCallback = (childData) => {
this.setState({ response: childData });
};
render() {
let applicableCriteria = [];
// Push all possible criteria to list
for (let q = 0; q < Records.length; q++) {
for (let c = 0; c < Records[q].criterion.length; c++) {
applicableCriteria.push(Records[q].criterion[c]);
}
}
const idVals = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const resData = [
{ id: 1, response: "" },
{ id: 2, response: "" },
{ id: 3, response: "" },
{ id: 4, response: "" },
{ id: 5, response: "" },
{ id: 6, response: "" },
{ id: 7, response: "" },
{ id: 8, response: "" },
{ id: 9, response: "" },
];
const { response } = this.state;
return (
<div>
<h1> Questionnaire </h1>
<Child
parentCallback={this.handleCallback}
questionText={Records[0].question}
/>
{response}
</div>
);
}
}
export default QF;
下面是子组件:
import React from "react";
class Child extends React.Component {
onTrigger = (event) => {
this.props.parentCallback(event.target.booleanResponse.value);
event.preventDefault();
};
render() {
return (
<div>
<label>{props.questionText}</label>
<form onSubmit={this.onTrigger}>
<select name="booleanResponse" id="booleanResponse">
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
<br></br>
<br></br>
<input type="submit" value="Submit" />
<br></br>
<br></br>
</form>
</div>
);
}
}
export default Child;
1条答案
按热度按时间6yt4nkrj1#
你正在试图访问子组件的render方法中的props,但是props还没有被定义.尝试这个: