Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

reactjs - React asynchronously set data from api in context provider

I am trying to set data from a graphql api, into a context provider wrapping my entire application. I have 2 calls to separate apis and I use that data in setting the provider. This is very challenging for me and I can find very little information online about it. This is what I have so far, it works but there is bugs when changing state values and sometimes the entire app reloads.

const App = () => {
  const [userRoles, setUserRoles] = useState([]);
  const [profileId, setProfileId] = useState(
    !getItem('profileId') ? null : getItem('profileId').toString()
  );

  const history = useHistory();

  const { isLoading, user, error } = useAuth0();
  const { data, loading: profilesLoading, error: searchProfilesError } = useQuery(
    SEARCH_PROFILE
  );

  useEffect(() => {
    if (!isLoading && !error) {
      // eslint-disable-next-line no-prototype-builtins
      const i = Object.entries(user).filter(([, value]) => value.hasOwnProperty('roles'));

      setUserRoles(i?.[0]?.[1]?.roles);
    }
  }, [user]);

  useEffect(() => {
    if (!profilesLoading && !searchProfilesError) {
      if (!userRoles.includes('SuperUser')) {
        setProfileId(data?.searchProfile?.items?.[0]?.id);
        setLocalProfile(data?.searchProfile?.items?.[0]?.id);
      }
    }
  }, [userRoles, data]);

  const handleChangeProfile = id => {
    setProfileId(id);
    setLocalStorageProfile(id);
  };

  if (isLoading || profilesLoading) return <Loading />;

  if (error)
    return (
      <Alert />
    );

  return (
    <UserProvider
      email={user?.email}
      roles={userRoles}
      setSelectedProfile={handleChangeProfile}
      profileId={profileId}
    >
      // Routes
    </UserProvider>
  );
};

The app is fairly large and I have included the important parts. If you need any more info let me know.

The way I've been doing it so far works but it is very buggy and sometimes re renders the entire app Im not sure why. Should i be using useReducer? Also the profile can be changed when calling handleChangeProfile from within the components of the provider.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...