简体   繁体   English

将代码从功能性 React 钩子转换为类组件

[英]Converting code from functional React hooks to class components

I'm currently trying to convert some hooks over to being inside class components in React.我目前正在尝试将一些钩子转换为 React 中的类组件。 I'm attempting to recreate the code that allows for session storage so that the app doesn't need to fetch from the API so much.我正在尝试重新创建允许会话存储的代码,以便应用程序不需要从 API 中获取太多信息。 Here is the code:这是代码:

useEffect(() => {
        if(sessionStorage.homeState){
            // console.log("Getting from session storage");
            setState(JSON.parse(sessionStorage.homeState));
            setLoading(false);
        }else{
            // console.log("Getting from API");
            fetchMovies(POPULAR_BASE_URL);
        }
    }, [])

    useEffect(() => {
        if(!searchTerm){
            // console.log("Writing to session storage");
            sessionStorage.setItem('homeState', JSON.stringify(state));
        }
    }, [searchTerm, state])

These are two useEffect hooks so it makes sense to me to go with a regular componentDidMount to get from the session storage.这是两个 useEffect 钩子,所以对我来说使用常规的 componentDidMount 从会话存储中获取是有意义的。 The only thing that I can't seem to figure out is how to recreate the second useEffect that sets session storage and fires only when searchTerm or state changes.我似乎无法弄清楚的唯一一件事是如何重新创建第二个 useEffect 设置会话存储并仅在 searchTerm 或状态更改时触发。 searchTerm and state are simply two properties of the state. searchTerm 和 state 只是状态的两个属性。 How could I implement this since componentDidMount only fires once when the app first mounts?由于 componentDidMount 仅在应用程序首次安装时触发一次,我该如何实现? Thanks谢谢

The only thing that I can't seem to figure out is how to recreate the second useEffect that sets session storage and fires only when searchTerm or state changes.我似乎无法弄清楚的唯一一件事是如何重新创建第二个 useEffect 设置会话存储并仅在 searchTerm 或状态更改时触发。 searchTerm and state are simply two properties of the state. searchTerm 和 state 只是状态的两个属性。

componentDidMount() is only one of methods used by the class components you can recreate the second hook with componentWillUpdate() or shouldComponentUpdate(). componentDidMount()只是类组件使用的方法之一,您可以使用 componentWillUpdate() 或 shouldComponentUpdate() 重新创建第二个钩子。

For example:例如:

componentWillUpdate(nextProps, nextState) {
  if (this.props.searchTerm !== prevProps.searchTerm) {
    ...
  }
}

You can check what lifecycle methods are available in class components by Googling "class component lifecycle" .您可以通过谷歌搜索“类组件生命周期”来检查类组件中可用的生命周期方法。

But as you can read in the comment to your questions Hooks can offer you more than class components, and recreating them is not trivial.但是,正如您在问题的评论中所读到的,Hook 可以为您提供的不仅仅是类组件,并且重新创建它们并非易事。 It is easier to move from the class component to the Hooks.从类组件移动到 Hooks 更容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Hooks将React类转换为功能组件。 如何解决“不受控制的输入”错误 - Converting React Class to Functional Components with Hooks. How to fix “uncontrolled input” error 在 react native 中将 class 组件转换为钩子形式 - Converting class components to hooks form in react native 没有React钩子,没有类组件而不是功能组件,此React代码的外观如何?(打开另一个窗口的按钮) - How would this React code look without React-hooks and with class components rather than functional components?(Button that open an additional window) 使用钩子将 React class 组件转换为功能组件:事件侦听器的问题 - Converting React class component to functional component with hooks : Problem with event listeners 用钩子反应功能组件 - 从用减速器编写的 function 获得响应 - React functional components with hooks - getting response from function written in reducer 使用钩子将 class 组件转换为功能组件 - Convert class component to functional components using hooks react挂钩中的useState转换为功能组件 - useState in react hooks converting to functional component 将 Modal 类组件转换为带有钩子的功能组件 - Converting a Modal class component to a functional component with hooks 使用 Hooks 将 Class 组件转换为功能组件 - Converting Class Component to Functional Component With Hooks React Class vs 功能组件:当使用钩子和功能组件时,我的一个功能不断重新渲染 - React Class vs Functional Component: When using hooks and functional components one of my functions constantly re-renders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM