[英]How to pass a Json array of objects from a child class component to parent class component in React?
Inside of the class component Trivia I have a currentQuestion
state variable that acts as an index.在class组件内部,我有一个
currentQuestion
state 变量作为索引。 I created another class component Questions that will read from a JSON file and I want to pass it to Trivia like this:我创建了另一个 class 组件问题,它将从 JSON 文件中读取,我想像这样将它传递给Trivia :
...
questions = <Questions index="{currentQuestion}"/>
return (
<div>
<div>{finished ? "You are done" : questions.question }</div>
...
And have each question to update everytime the state currentQuestion
is set above.并且每次在上面设置 state
currentQuestion
都要更新每个问题。 The problem is that the either the array of objects or the indexes are undefined for everything I try.问题是对象数组或索引对于我尝试的所有内容都未定义。 How do I pass this array to the parent component so it can receive the currentQuestion state and update the right question accordingly?
如何将此数组传递给父组件,以便它可以接收currentQuestion state 并相应地更新正确的问题?
The code I tried:我试过的代码:
import React from "react";
import "./styles.css";
import Questionsfile from "./Questionsfile.json";
export default function App() {
return (
<div className="App">
<h1>Hello Trivia about Elisa</h1>
<Trivia name="Elisa" />
</div>
);
}
export class Trivia extends React.Component {
constructor(props) {
super(props);
this.handleNextClick = this.handleNextClick.bind(this);
this.handleBackClick = this.handleBackClick.bind(this);
this.state = {
isFinished: false,
currentQuestion: 0
};
}
handleNextClick() {
this.setState({
isFinished: this.state.currentQuestion === 2 ? true : false,
currentQuestion: this.state.currentQuestion + 1
});
}
handleBackClick() {
this.setState({
currentQuestion: this.state.currentQuestion - 1
});
}
render() {
const finished = this.state.isFinished; // I have to use this
const currentQuestion = this.state.currentQuestion;
let buttonNext;
let buttonBack;
let questions;
if (currentQuestion < 3) {
buttonNext = (
<NextClickButton
name={currentQuestion === 2 ? "Submit" : "Next"}
onClick={this.handleNextClick}
/>
);
}
if (currentQuestion > 0 && !finished) {
buttonBack = <BackClickButton onClick={this.handleBackClick} />;
}
// The problem starts here
questions = <Questions index="{currentQuestion}"/>
return (
<div>
<div>{finished ? "You are done" : questions.question }</div>
{buttonBack}
{buttonNext}
</div>
);
}
}
function NextClickButton(props) {
return <button onClick={props.onClick}>{props.name}</button>;
}
function BackClickButton(props) {
return <button onClick={props.onClick}>Back</button>;
}
export class Questions extends React.Component {
render() {
return <div>{Questionsfile.questions[this.props.index]}</div>;
}
}
I see a couple issues in the code that you submitted.我在您提交的代码中看到了几个问题。
First, you don't need to wrap in a string when passing props:首先,在传递 props 时不需要用字符串包裹:
questions = <Questions index={currentQuestion}/>
Assuming that currentQuestion
is a number.假设
currentQuestion
是一个数字。
Next, <Questions... />
returns a React Component.接下来,
<Questions... />
返回一个 React 组件。 So, you can't just access its properties like this questions.question
, you should instead just place the JSX inline:所以,你不能像这样
questions.question
访问它的属性,你应该把 JSX 内联:
return (
<div>
<div>{finished ? "You are done" : <Questions index={currentQuestion}/> }</div>
{buttonBack}
{buttonNext}
</div>
);
}
}
Hope these comments help you get on the right track.希望这些评论可以帮助您走上正轨。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.