简体   繁体   English

反应 useContext useReducer state 挂钩错误

[英]React useContext useReducer state Hooks Error

import Axios from "axios";
import React, { createContext, useEffect, useReducer, useState } from "react";
import useAsync from "./useAsync";

const [datas, setDatas] = useState(null);

const fatchData = async () => {
  try {
    const response = await Axios.get(
      "https://jsonplaceholder.typicode.com/users"
    );
    setDatas(response.data);
  } catch (e) {
    console.log(e);
  }
};
useEffect(() => {
  fatchData();
  //eslint-disable-next-line
}, deps);
const AppReducer = (state, action) => {
  switch (action.type) {
    default:
      return state;
  }
};
export const GlobalContext = createContext(datas);

export const GlobalProvider = ({ children }) => {
  const [state, dispatch] = useReducer(AppReducer, datas);

  return (
    <GlobalContext.Provider value={{ datas }}>
      {children}
    </GlobalContext.Provider>
  );
};

This is the error.这是错误。 This could happen for one of the following reasons.这可能由于以下原因之一而发生。

  1. You might have missed versions of React and the Render (such as React DOM) You might be breaking the Rules of Hooks You might have more than one copy of React in the same app.您可能错过了 React 和 Render 的版本(例如 React DOM) 您可能违反了 Hooks 规则 您可能在同一个应用程序中拥有多个 React 副本。

I want to put the data that received API as the default value of useReducher.我想把收到的数据API作为useReducher的默认值。 But there was a hooks error.但是有一个钩子错误。 Can't you delete or modify data received using useContext?不能删除或修改使用 useContext 接收到的数据吗?

Will it work normally if I use Graphql instead of restAPI?如果我使用 Graphql 而不是 restAPI,它会正常工作吗? And is there a good way to manage data received from servers using useContext?有没有一种很好的方法来管理使用 useContext 从服务器接收的数据?

You need to move every hooks into your function component's body.您需要将每个挂钩移到 function 组件的主体中。 If they are outside of that technically they are violating the rules of hooks.如果他们在技术上超出了这个范围,那么他们就违反了钩子的规则。 Read further about Rules of Hooks :进一步阅读钩子规则

Don't call Hooks inside loops, conditions, or nested functions.不要在循环、条件或嵌套函数中调用 Hooks。 Instead, always use Hooks at the top level of your React function. By following this rule, you ensure that Hooks are called in the same order each time a component renders.相反,请始终在 React 的顶层使用 Hooks function。通过遵循此规则,您可以确保每次组件呈现时都以相同的顺序调用 Hooks。 That's what allows React to correctly preserve the state of Hooks between multiple useState and useEffect calls.这就是让 React 在多个useStateuseEffect调用之间正确保留 Hooks 的 state 的原因。

Try as the following:尝试如下:

import Axios from "axios";
import React, { createContext, useEffect, useReducer, useState } from "react";
import useAsync from "./useAsync";

export const GlobalContext = createContext(null);

export const GlobalProvider = ({ children }) => {
  const [state, dispatch] = useReducer(AppReducer, datas);
  const [datas, setDatas] = useState(null);

  const fatchData = async () => {
    try {
      const response = await Axios.get(
        "https://jsonplaceholder.typicode.com/users"
      );
      setDatas(response.data);
    } catch (e) {
      console.log(e);
    }
  };

  useEffect(() => {
    fatchData();
    //eslint-disable-next-line
  }, deps);

  const AppReducer = (state, action) => {
    switch (action.type) {
      default:
        return state;
    }
  };

  return (
    <GlobalContext.Provider value={{ datas }}>
      {children}
    </GlobalContext.Provider>
  );
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM