简体   繁体   English

如何将 Json 对象数组从子 class 组件传递给 React 中的父 class 组件?

[英]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.希望这些评论可以帮助您走上正轨。

For long term development you should use Redux for this purpose, yet still if you have few components you can use callback function .对于长期开发,您应该为此使用 Redux ,但如果您的组件很少,您仍然可以使用回调 function some how like this example一些多么喜欢这个例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应中将对象数组从父组件传递到子组件 - How to pass Array of Objects from Parent Component to Child Component in react 如何正确地将数组从父 React 类组件状态传递给使用钩子编写的子组件? - How to properly pass an array from a parent React class component state to a child component written with hooks? 如何将值从子功能组件传递给父类组件? - How to pass value from a child functional component to parent class component? 如何将值从子组件(基于函数的组件)传递到父组件(基于类的组件) - How to pass value from child component(function based component )to parent component(class based component) 如何使用反应钩子将对象数组从子组件发送到父组件并存储在父对象中? - How to send array of objects data from child component to parent component and store in the parent object using react hooks? 将数据从子 class 组件发送到反应中的父功能组件 - Paasing data from a child class component to a parent functional component in react 反应:如何从其父组件更改子组件(功能组件,而不是 class 组件)的 state? - react: How to change the state of a child component(function component, not class component) from its parent? 将子 class 方法传递给父功能组件 - React pass child class method to parent functional component 如何将值从父组件传递给子组件(反应) - How to pass value from parent component to child component (react) 如何使用 React Native 中的 props 将数组从父组件传递到子组件? - How to pass an array from a parent component to child component using props in React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM