首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何提高公理的速度?

如何提高公理的速度?
EN

Stack Overflow用户
提问于 2019-01-07 21:16:47
回答 1查看 3.8K关注 0票数 2

因为我刚开始使用axios,所以我在使用它时通常会遇到麻烦。具体来说,我现在正在制作一个react-infinite-scroll功能,但是当我将其速度与其他站点进行比较时,我的帖子(react-infinite-scroll功能)将被缓慢地显示出来。那么我想这个问题是由两个原因引起的

  1. 我没有正确地使用axios
  2. 有一件事使axios加速了速度,但我不使用它 这是我的代码,请给我一些建议,以提高我的http请求速度。

谢谢你阅读我的问题!

代码语言:javascript
复制
class MainPage extends Component {

    constructor(props) {
        super(props)
        axios.get("http://127.0.0.1:8000/api/question")
        .then(res => {
            this.setState({
                AnswerPostMultiList: res.data
            })
          }
        )
        .catch(err => {
            console.log(err)
        })
    }

    state = {
        AnswerPostMultiList : []
    }

    componentDidMount() {
        window.addEventListener("scroll", this.handleScroll);
    }

    componentWillUnmount() {
        window.removeEventListener("scroll", this.handleScroll);
    }

    handleScroll = () => {

        console.log("scroll is executing")

        const { innerHeight } = window;
        const { scrollHeight } = document.body;

        const scrollTop =
          (document.documentElement && document.documentElement.scrollTop) ||
          document.body.scrollTop;

        if (scrollHeight - innerHeight - scrollTop < 1000 && !this.props.isLoading["isLoading"]) {
          this.props.onIsLoading() #To prevent this code from calling back continuously, change the value of this.props.isLoading["isLoading"] to false 

              axios.get("http://127.0.0.1:8000/api/question")
              .then(res => {

              this.setState({
                  AnswerPostMultiList: this.state.AnswerPostMultiList.concat(res.data)
              })
              this.props.onIsLoading() #change the value of this.props.isLoading["isLoading"] to true 
            }
          )
          .catch(err => {
              console.log(err)
          })
        }
      };

    render() {
        return(
            <>

                <PageHeader />
                <div className="find_members">
                { this.state.AnswerPostMultiList.map((answerpost,index) => {
                    return <AnswerPostMulti question={answerpost.question_text} q_owner={answerpost.question_owner} answer={answerpost.answer_image} a_owner={answerpost.answer_owner} key={index} />
                  })
                }
                </div>

            </>
        )
    }
}



const mapDispatchToProps = (dispatch) => ({
    onIsLoading: () => {
        dispatch(isLoadingActions.isLoading())
    }
  })

  const mapStateToProps = state => ({
      isLoading: state.isLoading
  })


export default connect(mapStateToProps, mapDispatchToProps)(MainPage)
EN

回答 1

Stack Overflow用户

发布于 2020-02-25 17:51:12

调用componentDidMount(){}. axios调用的最佳位置是在应用程序加载过程将按以下顺序进行-->初始呈现->稍后调用的componentDidMount方法。因此,在这里,您的应用程序框架将被加载,然后您可以获取数据并使用它到您的应用程序框架。

代码语言:javascript
复制
componentDidMount() {
         axios.get("http://127.0.0.1:8000/api/question")
        .then(res => {
            this.setState({
                AnswerPostMultiList: res.data
            })
          }
        )
        .catch(err => {
            console.log(err)
        });
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54082023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档