[英]My reducer function is not displaying the data I'm fetching in react. i dont know what im doing wrong
I'm having trouble diplaying the data im fetching from an API.我无法显示从 API 获取的数据。 when I fetch the data from my app component with componentDidMount it displays all the products.
当我使用 componentDidMount 从我的应用程序组件中获取数据时,它会显示所有产品。 but as I try to move the state to context API, I cant seem to get all the products to display.
但是当我尝试将 state 移动到上下文 API 时,我似乎无法显示所有产品。 I'm new at this.
我是新来的。 i need help seeing what I'm missing.
我需要帮助看看我错过了什么。
This is my reducer function
这是我的减速机 function
import { LOAD_ITEMS } from '../types'
export default (state, action) => {
switch(action.type) {
case LOAD_ITEMS:
return {
...state,
products: [...action.payload]
}
default:
return state
}
}
Here in my ProductState, I'm trying to fetch data from the API to display it
在我的 ProductState 中,我试图从 API 获取数据以显示它
import React, { useReducer } from 'react'
import ProductContext from './ProductContext'
import productReducer from './productReducer'
import {
LOAD_ITEMS, INCEASE, DECREASE, ADD_ITEM, REMOVE_ITEM, CLEAR, CHECKOUT
} from '../types'
const ProductState = props => {
const initialState = {
products: []
}
const [state, dispatch] = useReducer(productReducer, initialState)
const loadProducts = async () => {
await fetch('https://fakestoreapi.com/products')
.then(res => res.json())
.then(data => {
dispatch({
type: LOAD_ITEMS,
payload: data
})
})
}
return <ProductContext.Provider value={{
products: state.products,
loadProducts
}}>
{props.children}
</ProductContext.Provider>
}
export default ProductState
This is where I'm importing the productcontext to display the fetched data in my component
这是我导入 productcontext 以在我的组件中显示获取的数据的地方
import React, { useContext } from 'react'
import ProductItem from './ProductItem'
import styles from './ProductsGrid.module.scss';
import ProductContext from '../../context/products/ProductContext'
const Products = () => {
const productContext = useContext(ProductContext)
const { products } = productContext
return (
<div className={styles.p__grid}>
{products.map(product => (
<ProductItem key={product.id} product={product}/>))
}
</div>
<div className={styles.p__footer}>
</div>
</div>
)
}
export default Products
And this is the root App.js file where I import the ProductState.
这是我导入 ProductState 的根 App.js 文件。
import React, { Component, Fragment } from "react";
import {BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Header from './components/header/Header'
import Store from './components/product/index'
import About from './components/pages/About'
import Cart from './components/cart/Cart'
import ProductState from './context/products/ProductState'
class App extends Component {
// state={
// products: []
// }
// async componentDidMount() {
// try {
// const res = await fetch('https://fakestoreapi.com/products')
// const resData = await res.json();
// this.setState({ products: resData })
// } catch (err) {
// console.error(err);
// }
// }
render() {
return (
<ProductState>
<Router>
<Switch>
<Fragment>
<Header />
<div className='container'>
<Route exact path='/' component={Store} />
<Route path='/about' component={About} />
<Route path='/cart' component={Cart} />
</div>
</Fragment>
</Switch>
</Router>
</ProductState>
);
}
}
export default App;
The image below is how it displays when I fetch the data from my app.js
下图是我从 app.js 获取数据时的显示方式
The problem seems to be your loadProducts
function:问题似乎是您的
loadProducts
function:
You are using the async keyword incorrectly!您错误地使用了 async 关键字! It should work when you write
它应该在你写的时候工作
const loadProducts = async () => {
const res = await fetch('https://fakestoreapi.com/products');
const data = await res.json();
dispatch({
type: LOAD_ITEMS,
payload: data
});
}
As you can see, when using async you no longer have to use the usual Promise.then
syntax.如您所见,使用 async 时,您不再需要使用通常的
Promise.then
语法。 You might want to give this MDN page on async await a read.您可能希望在 async await 上阅读此 MDN 页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.