score:3
Accepted answer
Inside your functional components you can use the new useDispatch and useSelector features which is a easier to use.
Can you replace you CatalogProducts code with this, import the other necessary imports and try?
import React, { useEffect, useCallback } from "react";
import { useSelector, useDispatch } from "react-redux";
//other imports
const CatalogProducts = () => {
const classes = useStyles();
const dispatch = useDispatch();
const products = useSelector(state => state.products);
console.log("Products: ", products);
const fetchProducts = useCallback(() => {
dispatch(actions.getProductsAction());
}, [dispatch]);
useEffect(() => {
fetchProducts();
}, [fetchProducts]);
const items = products.map((product, index) => {
return (
<Grid item xs={12} sm={6} md={3} key={index}>
<CatalogProduct product={product} />
</Grid>
);
});
if (!products) {
return <p>Loading ...</p>;
} else if (products.length === 0) {
return <div>No products found</div>;
} else {
return (
<Container maxWidth="xl" className={classes.root}>
<Grid
container
direction="row"
justify="center"
alignItems="center"
spacing={3}
>
{items}
</Grid>
</Container>
);
}
};
export default CatalogProducts;
Sample codesandbox using jsonplaceholder fake api.
Source: stackoverflow.com
Related Query
- SetState with react hook and redux after async call to axios API
- React Hook useEffect : fetch data using axios with async await .api calling continuous the same api
- React hook form with redux and async request
- How to integrate axios api in react js with async and await using react hooks
- What is the correct way to React Hook Fetch Api and store to Redux and retrieve it with Selector immediately?
- Failed call to Deezer API with React and axios
- How to setState in React when async mapping through array and API call
- React Router, Redux and async API call
- how to write one GET api call using axios instance with and without params in React JS
- Cannot read property '.then' of undefined when testing async action creators with redux and react
- Paginate date-specific results from an API with React and Redux
- Async Image Load with React and Redux
- how and when to call a react component methods after state change from redux
- Using different API url for development and production with React and axios
- How to test state update and component rerender after async call in react
- React Redux with redux-observable use the router to navigate to a different page after async action complete
- redux react strategy with async data and interaction
- React useState hook not updating with axios call
- get latest state value after setState hook within async await block in react
- react redux async action call next function after finishing the function
- React with Redux Axios get request error issue with laravel 5.2 api server
- React hook not updating after the API call
- React setState in an axios call after switching pages quickly
- Testing async behavior in React with Axios and Jest
- React redux hook useDispatch function how can I call something after it finish
- React Router Link with params not reloading page with new data from componentDidMount and Redux axios data fetching
- React and redux usage with async data fetching
- React redux frontend not fetching data from Django API with axios
- React Redux - Waiting for async api call to finish before next action is dispatched
- React with Redux Saga - How to get data back from axios call
More Query from same tag
- Error cypress and paypal sdk "react-paypal-js"
- react-konva set a shape at the center of stage
- ReactJS Routes not working with Apache server
- Pass initialValue as prop to redux-form field
- How to retrieve data from 2d array react.js
- React 'useEffect' hook replaces how many lifecycle methods?
- When using getDerivedStateFromProps (React) getting error: Cannot read property 'setState' of null
- Not able to send different callback parameter from multiple child components (same component) to parent
- Semantic UI React change style placeholder text CSS
- Why I am getting `this.props.data is undefined` error?
- How to fix "send action after loading, problem with loop"
- Defining variables in defaultProps in Reactjs
- How to show success or error message based on the response of the api in React js
- The npm link shows errors when importing a component in typescript
- How to apply a pattern to material ui input?
- My ArrayList shows empty when looping throug Object.keys
- Javascript Mapping Nested Arrays
- bufferGeometry and shadow react-three-fiber
- React.useMemo does not update the data
- Does React.useCallback memoize curried functions?
- React use different imported Component based on a prop
- Passing an array to a React component and then accessing values within that array in the child component
- How to import and play all audio files from a directory in React?
- How to use React Hooks with video.js?
- "Cannot use import statement outside a module" error when deploying to Heroku
- Prettier moves paren in setState, triggers no-sequence. How can I improve setState?
- Fetch request in class component is not returning data or updating state
- How to add script to React component and use as constructor function
- Recharts 2.0.8 <ResponsiveContainer> </ResponsiveContainer> not working in IE11
- Stop previous and next button increment at a certain value - ReactJS