简体   繁体   English

我需要帮助尝试在 React 游戏中连接我的不同组件

[英]I need help trying to connect my different components in a React game

this is my first question on here so I hope I am doing this right.这是我在这里的第一个问题,所以我希望我做对了。 I am working on a trivia game using React and the API Open Trivia Database.我正在使用 React 和 API Open Trivia Database 开发一个琐事游戏。 I made two separate pages for the components Questions and Categories but I can't figure out how to get them onto the main page... and frankly where to go from there.我为组件问题和类别制作了两个单独的页面,但我不知道如何将它们放到主页上......坦率地说,从那里到 go 的位置。 Thank you for your time.感谢您的时间。

Here is my main page:这是我的主页:

import React from 'react'
import './App.css'
// import axios from 'axios'
import Questions from './components/questions'
import Categories from './components/categories'

class App extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      questions: [],
      categories: []
      // score: 0,

    }
  }

  render () {
    return (
      <div className='App'>
        <h2>Trivia Game</h2>
        {this.state.Questions}

      </div>
    )
  }
}
export default App

and here is the Question page:这是问题页面:

import React from 'react'
import axios from 'axios'

class Questions extends React.Component {
  constructor () {
    super()
    this.state = {
      questions: []

    }
  }

  componentDidMount () {
    axios
      .get('https://opentdb.com/api.php?amount=10')
      .then(response => {
        console.log(response.data)
        this.setState({
          questions: response.data.results
        })
      })
  }

  componentWillUnmount () {
    console.log('componentDidUnmount')
  }

  render () {
    return (
      <div className='Questions'>
        <h2>Questions</h2>
        <ul>
          {this.state.questions.map((question, index) => (
            <li key={index}>{question.question}</li>
          ))}
        </ul>
      </div>
    )
  }
}
export default Questions

It looks like you're importing your Questions and Categories components to the App.js file but you're not rendering them.看起来您正在将问题和类别组件导入 App.js 文件,但您没有呈现它们。

Your return statement should look like so:您的退货声明应如下所示:

    return (
      <div className='App'>
        <h2>Trivia Game</h2>
         <Questions questions={this.state.questions} />
         <Categories categories={this.state.categories}/>

      </div>
    )

You'll also want to pass your app slice of state to the respective components.您还需要将 state 的应用程序切片传递给相应的组件。 I noticed that you have questions array in the App state and the Questions state.我注意到您在 App state 和 Questions state 中有问题数组。 Usually you should try to avoid duplicating state and keep it at the highest needed component, in this case it looks to be App.js通常你应该尽量避免重复 state 并将它放在最需要的组件上,在这种情况下它看起来是 App.js

You've already imported them in your main page, now you just have to plug them in:您已经在主页中导入了它们,现在您只需将它们插入:

render () {
    return (
      <div className='App'>
        <h2>Trivia Game</h2>
        //   {this.state.Questions}   not sure what you wanted to do with this
        <Categories />
        <Questions />
      </div>
    )
  }

You can also pass props to each of them by adding an attribute afterwards...see here您还可以通过在之后添加属性来将道具传递给每个道具......请参阅此处

This generally looks good, but I think you could probably use more research on how to use state and props in React.这通常看起来不错,但我认为您可能需要对如何在 React 中使用stateprops进行更多研究。

Your component and component both work perfectly, you just need to learn to sync them up.您的组件和组件都可以完美运行,您只需要学习同步它们即可。 Right now, there isn't any interaction between the two so there is no need for a constructor in either file and, at least for now, I would keep anything with "questions" out of the state in the main component- let the Questions component do the heavy lifting there.现在,两者之间没有任何交互,因此在任何一个文件中都不需要构造函数,至少现在,我会在主要组件中的 state 中保留任何带有“问题”的内容 - 让问题组件在那里做繁重的工作。

To simplify the code, your main page can look like this:为了简化代码,您的主页可以如下所示:

import React from 'react'
import './App.css'
import Questions from './Questions'

class App extends React.Component {
  render () {
    return (
      <div className='App'>
        <h2>Trivia Game</h2>
        <Questions/>
      </div>
    )
  }
}

And the Questions file can remain the same, except with a quick simplification to the constructor function问题文件可以保持不变,除了对构造函数 function 进行快速简化

import React from 'react'
import axios from 'axios'

class Questions extends React.Component {
    state = {
      questions: []
    }

  componentDidMount () {
    axios
      .get('https://opentdb.com/api.php?amount=10')
      .then(response => {
        console.log(response.data)
        this.setState({
          questions: response.data.results
        })
      })
  }

  componentWillUnmount () {
    console.log('componentDidUnmount')
  }

  render () {
    return (
      <div className='Questions'>
        <h2>Questions</h2>
        <ul>
          {this.state.questions.map((question, index) => (
            <li key={index}>{question.question}</li>
          ))}
        </ul>
      </div>
    )
  }
}
export default Questions

As your code gets more complex, you might need to add in a constructor to pass props and have these functions components interact, but it's probably best to build up to that and start as simple as possible.随着您的代码变得越来越复杂,您可能需要添加一个构造函数来传递 props 并使这些函数组件交互,但最好建立起来并尽可能简单地开始。

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

相关问题 需要帮助让我的连接四个游戏注册四个连续 - Need help getting my connect four game to register four are in a row 我正在尝试在我的反应应用程序中添加图标,但此方法不起作用需要帮助和指导 - I am trying to add icons in my react app but this method not working Need help and guidance 需要帮助尝试在本机反应中刷新我的令牌 - Need help trying to refresh my token in react native 我需要帮助才能找到我的JavaScript迷你游戏中的错误(我没有得到的三个错误) - I need help finding errors in my JavaScript mini-game (Three error that i don't get) 需要帮助完成我必须制作彩色游戏的学校作业 - Need help finishing my school assignment where i have to make a color game 需要帮助修复我的Conway Life of Life代码 - Need help fixing my Conway Game of Life code 在我的石头剪刀布游戏中需要帮助 javascript - Need help in my rock paper and scissors game javascript 需要帮助,尝试使用 TypeScript 将项目从 React 转换为 React - Need help, trying to convert a project from React to React with TypeScript 我需要用JavaScript制作的迷你游戏的帮助 - I need help on a mini game that i am making in javascript 我是否需要为反应组件使用继承? - Do I need use inheritance for react components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM