我对问题的回答有效,包含在我的回答中的Pedrag的评论也有效

我正在进行http请求调用,并且不显示页面,直到http响应返回(请求调用为getRoadmaps)。我有一个称为“正在加载”的redux状态,以确定响应是否回来了?关于如何在我的代码中实现的任何见解?我的其他还原状态是​​路线图,其中包含一个也称为路线图的对象(对不起,抱歉)。

附言我看到了说做的一些答案(伪代码):如果未加载,则显示加载屏幕,否则加载渲染中的普通屏幕。但是我的问题并不存在于渲染中,而是存在于我做的componentWillMount()中

this.props.getRoadmaps()


然后三行我在哪里

console.log(this.displayRoadmap[0].roadmap[0])


如果getRoadmaps()完成,应该可以成功记录路线图,好像调用了getRoadmaps(),但随后程序继续运行而没有完全完成getRoadmaps(),这导致我的displayRoadmap无法定义。这也会导致一些怪异的现象,例如,如果我从一个屏幕转到该组件,它将始终有效,但是如果刷新页面,它将无法正常工作

import React, { Component } from "react";
import Tree from "./Tree";
import TreeSidebar from "./TreeSidebar";
import { withRouter } from "react-router-dom";
import { connect } from "react-redux";
import { getRoadmaps } from "../actions/roadmapAction";
class OneRoadmap extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: this.props.roadmap.loading,
      roadmapName: "click a white circle to show more!",
      roadmapImg:
        "https://cdn3.iconfinder.com/data/icons/harmonicons-06/64/plus-circle-512.png",
      roadmapDetail: [{ text: "Click on something" }, { text: "hi" }],
      treeData: //deleted,just the default big json object
   };
  }

  componentWillMount() {
    this.props.getRoadmaps();

    var location1 = this.props.location.pathname;
    var n = location1.slice(9);

    var current_roadmaps = this.props.roadmap.roadmaps;

    this.displayRoadmap = current_roadmaps.filter(
      eachRoadmap => eachRoadmap._id == n
    );
    // now we have a roadmap
    console.log(this.displayRoadmap[0].roadmap[0]);
    this.setState({ treeData: this.displayRoadmap[0].roadmap[0] });
    console.log("is good");
    console.log(this.props.loading);
  }

  componentDidMount() {}

  handle_on_click_change = d => {
    this.setState({ roadmapName: d.data.name });
    this.setState({ roadmapImg: d.data.img });
    if (d.data.details == undefined) {
      this.setState({
        roadmapDetail: [{ text: "The author did not put anything here" }]
      });
    } else {
      this.setState({ roadmapDetail: d.data.details });
    }
  };

  render() {
    return (
      <div>
        {console.log(this.state.loading)}
        <ul>
          <li styles={{ float: "left" }}>
            <div>
              {console.log(this.state.treeData)}
              <Tree
                on_click_change={this.handle_on_click_change}
                roadmapData={this.state.treeData}
              />
            </div>
          </li>
          <li styles={{ float: "right" }}>
            <div>
              <TreeSidebar
                displayName={this.state.roadmapName}
                displayImg={this.state.roadmapImg}
                displayDetail={this.state.roadmapDetail}
              />
            </div>
          </li>
        </ul>
      </div>
    );
  }
}

const mapStateToProps = state => ({
  roadmap: state.roadmap
});

export default connect(
  mapStateToProps,
  { getRoadmaps }
)(OneRoadmap);

最佳答案

就我个人而言,我是如何使用CSS样式的,我不知道这是否是执行此操作的最佳方法,但对我有用,我会做类似的事情

this state = {
  loaderStyle: 'block',
  contentStyle: 'none'


}

componentDidMount() {
    If(this.displayRoadmap[0].length > 0) {
       this.setsState({loaderStyle: 'none', contentStyle:
          'block:
    } else /// do something

};


在我的渲染函数中,我将执行此操作

 <Loader style={{display: this.state.loaderStyle}}/>

 <Content style={{display: this.state.contentStyle}}>.
 </Content>

07-24 18:19