简体   繁体   English

如何在功能组件 React 中使用本地存储

[英]How use Local Storage in Functional Component React

How can I use LocalStorage in a functional component like this如何在这样的功能组件中使用 LocalStorage

I know how do this in a class component but can I solve this problem in this case?我知道如何在 class 组件中做到这一点,但在这种情况下我可以解决这个问题吗?

ERROR: TypeError: repositories is not a function错误:TypeError:存储库不是 function

export default function Main() {
  const [newRepo, setNewRepo] = useState('');
  const [repositories, setRepositories] = useState([]);
  const [clearInput] = useState([]);
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    repositories(localStorage.getItem('repositories'));

    if (repositories) {
        setRepositories(JSON.parse(repositories));
    }
  }, [repositories]);

  useEffect((_, prevState) => {
    if (prevState.repositories !== repositories) {
        localStorage.setItem('repositories', JSON.stringify(repositories));
    }
  });

In your first useEffect , the repositories is your state which an array.在您的first useEffect中, repositories是您的 state 数组。 Not a function.不是 function。

Also, in your second useEffect you need to make correction to the way you access the prevState in hooks.此外,在您的second useEffect中,您需要更正您在挂钩中访问 prevState 的方式。

Fix for 1st useEffect修复第一次使用效果

export default function Main() {
  const [newRepo, setNewRepo] = useState('');
  const [repositories, setRepositories] = useState([]);
  const [clearInput] = useState([]);
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    const localRepoItems = localStorage.getItem('repositories');

    if (localRepoItems) {
        setRepositories(JSON.parse(localRepoItems));
    }

  }, []); // do not give the dependency as repositories as it will go to infinite loop

  });

To obtain previous state in hooks, you can write a little custom hook: Like this:要在 hooks 中获取之前的 state,可以编写一个小自定义 hook:像这样:

export const usePrevious = value => {
  const ref = React.useRef();
  useEffect(() => {
    ref.current = value;
  });
  return ref.current;
}

Usage in your component:在您的组件中使用:

const prevRepositories = usePrevious(repositories);
useEffect(() => {
    if (prevRepositories.length !== repositories.length) {
        localStorage.setItem('repositories', JSON.stringify(repositories));
    }
  }, [repositories]);

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

相关问题 如何在反应功能组件中使用动画 svg? - How to use animated svg in react functional component? 如何在 React 功能组件中使用 Monaco Editor? - How to use Monaco Editor in a React functional component? React:如何在功能组件中使用 setState? - React: How to use setState inside functional component? 如何在 React Native 的一个功能组件中一起使用道具和导航 - How to use props and navigation together in one functional component in react native 如何在反应功能组件中正确使用 useRef 挂钩? - How to use useRef hook properly in react Functional Component? 如何在 React js 中的功能组件外部使用 useHistory() 挂钩? - How to use useHistory() hook outside functional component in React js? 如何正确使用React.StatelessFunctionalComponent <Props> 在无状态功能组件上? - How to properly use React.StatelessFunctionalComponent<Props> on a stateless functional component? 如何在异步的反应功能组件中使用 useState - How to use useState inside a react functional component which is async 如何在 React 功能组件中使用 Splide 同步方法 - How to use Splide sync method in a React functional component 如何在反应原生组件中显示来自本地存储的文件预览? - How to display a preview of file from local storage in a react native component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM