Reputation: 65
I am new to react. In my react app, I have a product component listing all the product items. The fetch function
from the server is handled by redux-thunk
. But I can not access the state
in my components, keep noticing that the variable - 'products'
is undefined
. is it the correct way to fetch data like this?
In my Products.js
,
import Spinner from '../../components/Spinner/Spinner';
import { connect } from 'react-redux';
import { GetProductDataAction } from '../../redux/Product/Product.actions';
class Products extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: true,
products: [],
}
}
async componentDidMount() {
const product = await this.props.getProductsData();
this.setState({
products: this.state.product,
loading: false
})
}
render() {
const { product } = this.state;
return (
<>
{
this.state.loading || !this.state.products ?
<Spinner />
:
<section className="products-overview-container">
<div className="product-container">
{
products.map(({ ...otherProps }) => {
return <Product key={otherProps._id}
{...otherProps} />
})
}
</div>
</section>
}
</>
);
}
}
const mapStateToProps = (state) => ({
products: state.product,
});
const mapDispatchToProps = (dispatch) => {
return {
getProductsData: () => {
dispatch(GetProductDataAction());
},
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(Products);
In my Product.action.jsx,
import axios from "axios";
import ProductActionTypes from './Product.types';
export const GetProductDataAction = () => {
return async (dispatch) => {
try {
const res = await axios.get("http://127.0.0.1:3000/products");
const { data } = res;
dispatch({
type: ProductActionTypes.GET_PRODUCT_DATA_SUCCESS,
payload: data
});
console.log('Getting Tour Data Successfully');
} catch (error) {
// error code
}
};
};
Upvotes: 0
Views: 684
Reputation: 6582
here is the changes you need to make:
class Products extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: true,
products: []
};
}
async componentDidMount() {
const products = await this.props.getProductsData();
this.setState({
products,
loading: false
});
}
render() {
const { products } = this.state;
return (
<>
{this.state.loading || !this.state.products ? (
<Spinner />
) : (
<section className="products-overview-container">
<div className="product-container">
{products.length && products.map(({ ...otherProps }) => {
return <Product key={otherProps._id} {...otherProps} />;
})}
</div>
</section>
)}
</>
);
}
}
Upvotes: 1
Reputation: 742
Following official document of Redux Thunk.
Redux Thunk middleware allows you to write action creators that return a function instead of an action. The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. The inner function receives the store methods dispatch and getState as parameters.
function incrementIfOdd() {
return (dispatch, getState) => {
const { counter } = getState();
if (counter % 2 === 0) {
return;
}
dispatch(increment());
};
}
That mean the second param should be getState
. You can access state from here.
Upvotes: 1