在 React JS 中使用路由器时如何使用 Context API 传递状态
我有一个上下文 API 文件设置,它有一个状态和一个从 API 获取数据并设置状态的函数,我想将状态传递给我的其他组件.在我的 App.js 中,我使用 React-Router 来指定路由.如何在使用 React-Router 的同时使用 Context API 将状态传递给这些组件.
我的 ApiContext.js 文件如下所示:
import React, {useState, createContext } from 'react';导出 const ApiContext = createContext();export const ApiProvider = async (props) =>{常量 [数据,setData] = useState(null);const getURL = 'https://examplefetchsite.com';常量响应 = 等待 fetch(getURL).json();设置数据(响应);返回 (<ApiContext.Provider value={[data, setData]}>{props.children}</ApiContext.Provider>);}
我的 App.js 的返回是这样的:
返回 (<ApiProvider><路由器><导航栏/><开关><Route path="/" 确切组件={ Dashboard }/><Route path="/create" 组件={ 创建 }/><Route path="/view" 组件={View}/></开关></div></路由器></ApiProvider>)
解决方案 就上下文本身而言,您不必更改提供程序中的任何内容,只需在子组件中执行以下操作:
import React, {useContext} from 'react'从./ApiContext"导入 {ApiContext}const仪表板=(道具)=>{const [数据,setData] = useContext(ApiContext)//你应该可以同时访问数据和setData返回 (//事物)}
但是在 ApiContext.js
中,您没有正确调用 API 请求.您应该使用 useEffect
仅在第一次渲染时获取数据.
import React, {useState, createContext, useEffect} from 'react';导出 const ApiContext = createContext();导出 const ApiProvider = (props) =>{常量 [数据,setData] = useState(null);使用效果(异步()=>;{const getURL = 'https://examplefetchsite.com';常量响应 = 等待 fetch(getURL).json();设置数据(响应);}, [])返回 (<ApiContext.Provider value={[data, setData]}>{props.children}</ApiContext.Provider>);}
I have a Context API file setup which has a state and a function which fetches data from an API and sets the state, and i want to pass the state down to my other components. In my App.js, I am using React-Router to specify the routes. How do i pass the state down to these components using Context API, whilst using React-Router.
My ApiContext.js file looks like this :
import React, {useState, createContext } from 'react';
export const ApiContext = createContext();
export const ApiProvider = async (props) => {
const [data, setData] = useState(null);
const getURL = 'https://examplefetchsite.com';
const response = await fetch(getURL).json();
setData(response);
return (
<ApiContext.Provider value={[data, setData]}>
{props.children}
</ApiContext.Provider>
);
}
My App.js's return looks like this :
return (
<ApiProvider>
<Router>
<div>
<NavBar />
<Switch>
<Route path="/" exact component={ Dashboard } />
<Route path="/create" component={ Create } />
<Route path="/view" component={View} />
</Switch>
</div>
</Router>
</ApiProvider>
)
解决方案
In terms of the context itself, you don't have to change anything in your provider and only do something like this in the child components:
import React, {useContext} from 'react'
import {ApiContext} from './ApiContext'
const Dashboard = (props) => {
const [data, setData] = useContext(ApiContext)
//you should have access to both data and setData
return (
//things
)
}
However in the ApiContext.js
you aren't calling the API request properly. You should use useEffect
to fetch the data only on the first render.
import React, {useState, createContext, useEffect} from 'react';
export const ApiContext = createContext();
export const ApiProvider = (props) => {
const [data, setData] = useState(null);
useEffect(async () => {
const getURL = 'https://examplefetchsite.com';
const response = await fetch(getURL).json();
setData(response);
}, [])
return (
<ApiContext.Provider value={[data, setData]}>
{props.children}
</ApiContext.Provider>
);
}
相关文章