在 AppContext 中存储 Axios 响应

Storing Axios response in AppContext

我正在尝试构建我网站的一部分,该部分将从 REST API 动态提取联系信息。此时我正在使用 https://jsonplaceholder.typicode.com.

中的示例 API

我正在尝试使用 axios,特别是 useAxiosmakeUseAxios 挂钩来获取 API,并将其存储在我创建的应用上下文中然后可以在整个站点使用,最好使用 useContext 挂钩。此外,我需要能够根据用户交互更新 API 调用,允许他们 select 一个位置,进行 API 更新,并将其存储在应用程序的上下文中以便它全部动态更新。

基本上,我已经得到了一个基于 this tutorial 的基本 useContext 场景,但我正在努力解决如何以我可以的方式存储 JSON 响应在 useContext 中引用它。这是我到目前为止最接近的事情:

AppContext.tsx

import React, { createContext, ReactNode } from 'react';

import axios from 'axios';
import { makeUseAxios } from 'axios-hooks';
import { useCookie } from 'hooks/use-cookie';

export const AppContext = createContext();

export const DealerContextProvider = ({children}: any) => {
  const useAxios = makeUseAxios({
    axios: axios.create({ baseURL: 'https://jsonplaceholder.typicode.com/users/' }),
  });

  const LOCAL_STORAGE_KEY_DEALER = '_selectedDealerInformation';

  const [cookie] = useCookie('one-day-location', '1');
  const [dealerInfo] = useAxios(`${cookie}`);

  return (
    <AppContext.Provider value={[dealerInfo]}>
      {children}
    </AppContext.Provider>
  );
};

还有我的 header 组件,我正在尝试访问它的地方:

import React, { ReactNode, useEffect, useState, useContext } from 'react';

import { AppContext } from 'components/app-context/AppContext';

import Logo from 'assets/svg/logo.svg';
import css from 'classnames';
import { Button } from 'components/button/Button';
import { Link } from 'components/link/Link';

import { NotificationBar } from '../notification-bar/NotificationBar';
import s from './Header.scss';
import { MenuIcon } from 'components/menu-icon/MenuIcon';
import { MainNav } from './navigation/MainNav';

interface HeaderProps {
  navigationContent: ReactNode;
}

export const Header = ({ navigationContent }: HeaderProps) => {
  const [scrolled, setScrolled] = useState(false);
  const [open, setOpen] = useState(false);

  const blogInfo = useContext(AppContext);
  const buttonLabel = blogInfo ? `${blogInfo.name}` : 'Find a Dealer';
  const buttonLink = blogInfo ? `tel:${blogInfo.name}` : '/find-a-dealer';

  useEffect(() => {
    const handleScroll = () => {
      const isScrolled = window.scrollY > 10;
      if (isScrolled !== scrolled) {
        setScrolled(!scrolled);
      }
    };

    document.addEventListener('scroll', handleScroll, { passive: true });

    return () => {
      document.removeEventListener('scroll', handleScroll);
    };
  }, [scrolled]);

  return (
    <>
      <NotificationBar notificationContent={navigationContent} />
      <header
        className={scrolled ? css(s.header, s.header__scrolled) : s.header}
        data-open={open ? 'true' : ''}
      >
        <nav className={s.header__navigation}>
          <ul className={s.header__container}>
            <li className={s.header__logo}>
              <Link to="/" className={s.header__link}>
                <Logo />
              </Link>
            </li>

            <li className={s.header__primary}>
              <MainNav navigationItems={navigationContent} />
            </li>

            <li className={s.header__utility}>
              <Button href={buttonLink}>{buttonLabel}</Button>
            </li>

            <li className={s.header__burger}>
              <MenuIcon onClick={() => setOpen(!open)} />
            </li>
          </ul>
        </nav>
      </header>
    </>
  );
};

我需要的是 header__utility 中的按钮动态显示所选经销商的名称和 phone 号码。我可以根据需要澄清任何事情,我是 React 的新手,并且仍在学习如何表达我需要的一切。

谢谢!

好吧,我在过去 24 小时内进行了大量挖掘工作,终于找到了解决方案。

我得到了上下文,为了清楚起见,现在命名为 ApiContext

ApiContext.tsx

import React, { createContext } from 'react';

import axios from 'axios';
import { makeUseAxios } from 'axios-hooks';
import { useCookie } from 'hooks/use-cookie';

const contextObject = {} as any;

export const context = createContext(contextObject);

const useAxios = makeUseAxios({
  axios: axios.create({ baseURL: process.env.GATSBY_API_ENDPOINT }),
});

export const ApiContext = ({ children }: any) => {
  const [cookie] = useCookie('one-day-location', '1');

  const [{ data }] = useAxios(`${cookie}`);

  const { Provider } = context;
  return <Provider value={data}>{children}</Provider>;
};

然后,为了跨组件使用它,我将 AppLayout 包装在 <ApiContext>:

AppLayout.tsx

import React, { ReactNode } from 'react';

import { ApiContext } from 'contexts/ApiContext';
import { graphql, StaticQuery } from 'gatsby';

import { Devtools } from '../devtools/Devtools';
import { Footer } from '../footer/Footer';
import { Header } from '../header/Header';
import s from './AppLayout.scss';

interface AppLayoutProps {
  children: ReactNode;
  location: string;
}

const isDev = process.env.NODE_ENV === 'development';

// tslint:disable no-default-export
export default ({ children }: AppLayoutProps) => {
  return (
    <StaticQuery
      query={`${NavQuery}`}
      render={(data) => (
        <>
          <ApiContext>
            <Header navigationContent={data.prismic.allNavigations.edges[0].node} />

            <div className={s.layout}>
              {children}

              <Footer navigationItems={data.prismic.allNavigations.edges[0].node} />

              {isDev && <Devtools />}
            </div>
          </ApiContext>
        </>
      )}
    />
  );
};

const NavQuery = graphql`
  query NavQuery {
    prismic {
      allNavigations {
        edges {
          node {
            ...NotificationBar
            ...NavigationItems
            ...FooterNavigationItems
          }
        }
      }
    }
  }
`;

在我的 Header 组件中,我可以使用 useContext 挂钩访问数据:

Header.tsx

import React, { ReactNode, useContext, useEffect, useState } from 'react';

import Logo from 'assets/svg/logo.svg';
import css from 'classnames';
import { Button } from 'components/button/Button';
import { Link } from 'components/link/Link';
import { MenuIcon } from 'components/menu-icon/MenuIcon';
import { context } from 'contexts/ApiContext';

import { NotificationBar } from '../notification-bar/NotificationBar';
import s from './Header.scss';
import { MainNav } from './navigation/MainNav';

interface HeaderProps {
  navigationContent: ReactNode;
}

export const Header = ({ navigationContent }: HeaderProps) => {
  const [scrolled, setScrolled] = useState(false);
  const [open, setOpen] = useState(false);

  const data = useContext(context);

  console.log(data);

  const buttonLabel =  data ? data.name : 'Find a Dealer';
  const buttonLink = data ? `tel:${data.phone}` : '/find-a-dealer';

  useEffect(() => {
    const handleScroll = () => {
      const isScrolled = window.scrollY > 10;
      if (isScrolled !== scrolled) {
        setScrolled(!scrolled);
      }
    };

    document.addEventListener('scroll', handleScroll, { passive: true });

    return () => {
      document.removeEventListener('scroll', handleScroll);
    };
  }, [scrolled]);

  return (
    <>
      <NotificationBar notificationContent={navigationContent} />
      <header
        className={scrolled ? css(s.header, s.header__scrolled) : s.header}
        data-open={open ? 'true' : ''}
      >
        <nav className={s.header__navigation}>
          <ul className={s.header__container}>
            <li className={s.header__logo}>
              <Link to="/" className={s.header__link}>
                <Logo />
              </Link>
            </li>

            <li className={s.header__primary}>
              <MainNav navigationItems={navigationContent} />
            </li>

            <li className={s.header__utility}>
              <Button href={buttonLink}>{buttonLabel}</Button>
            </li>

            <li className={s.header__burger}>
              <MenuIcon onClick={() => setOpen(!open)} />
            </li>
          </ul>
        </nav>
      </header>
    </>
  );
};

我仍在进行额外的优化,特别是消除加载页面时的闪烁,并在单击按钮时更新 API,利用 useCookie,这是一个自定义挂钩我已经建成了。希望这能让搜索此信息的其他人更加清楚,我花了好几个小时才确定解决方案。干杯。