将基于类的组件重构为功能组件

问题描述 投票:0回答:1

TL; DR:我试图将基于类的App组件重构为Functional组件,并且使用useEffect Hook产生了一些意外结果。

我对React还是很陌生,使用OMDB API搜索电影的应用程序版本是基于类的。它运行良好,因此我想在进入redux之前对钩子和上下文进行更多练习。

在我的课堂组件中,我有以下代码:

componentDidMount = () => {
    window.onscroll = this.handleScroll;
  };

  handleScroll = () => {
    if (!this.state.movies.loading) {
      if (getScrollDownPercentage(window) > 0.8) {
        const nextPage = this.state.movies.page + 1;
        this.searchMovies(this.state.movies.searchValue, nextPage);
        this.setState({
          ...this.state,
          movies: {
            ...this.state.movies,
            page: nextPage,
          },
        });
      }
    }
  };

  searchMovies = (str, page = 1) => {
    const existingMovies =
      this.state.movies.data.length && this.state.movies.searchValue === str
        ? this.state.movies.data
        : [];
    this.setState({
      ...this.state,
      movies: {
        ...this.state.movies,
        loading: true,
      },
    });
    axios
      .get(`${process.env.REACT_APP_ENDPOINT}s=${str}&page=${page}`)
      .then((response) => {
        if (response.data.Response === "True") {
          this.setState({
            ...this.state,
            movies: {
              ...this.state.movies,
              loading: false,
              searchValue: str,
              data: [...existingMovies, ...response.data.Search],
            },
          });
        }
      });
  };

重构后,我有以下感觉不太干净的代码(所以与我希望实现的相反)

到达页面末尾的那一刻,我得到不间断的axios调用流,在页面2和3之间切换

如果我没记错的话,那是行不通的,因为setState正在异步运行,并且我的页面上不断更新。

  useEffect(() => {
    window.onscroll = () => {
      handleScroll();
    };
    if (searchValue) {
      console.log(page);
      searchMovies(searchValue, page);
    }
    return () => {
      window.removeEventListener("scroll", handleScroll);
    };
  }, [searchValue, page]);

  const handleScroll = () => {
    if (!loading && searchValue) {
      if (getScrollDownPercentage(window) > 0.7) {
        setState({
          ...state,
          movies: {
            ...state.movies,
            page: page + 1,
          },
        });
      }
    }
  };

  const searchMovies = (str, page = 1) => {
    const existingMovies = data.length && prevSearchValue === str ? data : [];
    setState({
      ...state,
      movies: {
        ...state.movies,
        loading: true,
      },
    });
    axios
      .get(`${process.env.REACT_APP_ENDPOINT2}s=${str}&page=${page}`)
      .then((response) => {
        if (response.data.Response === "True") {
          setState({
            ...state,
            movies: {
              ...state.movies,
              loading: false,
              searchValue: str,
              data: [...existingMovies, ...response.data.Search],
            },
          });
        } else {
          setState({
            ...state,
            movies: {
              ...state.movies,
              loading: false,
              error: true,
            },
          });
        }
      });
  };

我希望我能包括所有的内容(也不要过多),以明确我的问题。寻找一些React向导。

编辑:指定的问题再加上一点粗体

javascript reactjs react-hooks infinite-scroll use-effect
1个回答
0
投票

快速解决方案将使用空数组作为第二个参数,以避免无限循环

useEffect(() => {
    //your code
  },[]);
© www.soinside.com 2019 - 2024. All rights reserved.