提问者:小点点

ReactJS!!! 我创建的函数并不是为了返回jsx,但是当我将它插入到render中时,它并没有显示任何内容


null

import React, { Component } from 'react'
import './styles/Mainbody.css'

import axios from 'axios';
import { FORTNITE_IO } from '../config'

class Mainbody extends Component {
    
    getTopPlayersInfo = (player) => {
        axios.get(`https://fortniteapi.io/lookup?username=${player}`, {
          headers: {
              Authorization: FORTNITE_IO
          }
      })
      .then(res => {
          const user_id = res.data.account_id
          axios.get(`https://fortniteapi.io/stats?account=${user_id}`, {
          headers: {
              Authorization: FORTNITE_IO
          }
        })
        .then(res => {
            const kills = res.data.global_stats.duo.kills + res.data.global_stats.solo.kills + res.data.global_stats.squad.kills
            const solo = res.data.global_stats.solo.placetop1
            const duo = res.data.global_stats.duo.placetop1
            const squad = res.data.global_stats.squad.placetop1
            return (
                <React.Fragment>
                    <p className="stats">Kills: {kills}</p>
                    <p className="stats">Solo Wins: {solo}</p>
                    <p className="stats">duos Wins: {duo}</p>
                    <p className="stats">Squad Wins: {squad}</p>
                </React.Fragment>
                )
        })
        .catch(err => err)
      })
      .catch(err => err)
    }
render() {
        return (
            <React.Fragment>
            {this.getTopPlayersInfo('jerugba')}
            </React.Frangment>
export default Mainbody;

null

React我创建的函数我本来想返回JSX,但是当我将它插入到render中时,它确实显示了任何东西。 有人能告诉我如何运行函数来显示我返回的JSX吗


共1个答案

匿名用户

别担心,修补这个非常简单^^

先用超级道具创建一个构造器道具。 在内部通过this.state创建状态:{}。

获取信息时,通过setState()将这些信息发送到状态;

现在,在最后的render()上,您可以通过this.state.kills(例如)选择要在div上呈现的内容

玩得开心^^