[英]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 中使用state和props进行更多研究。
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.