I am fetching data using axios and then map state to props with redux but I have a problem. If I dispatch the action in componentDidUpdate() the action execute indefinitely and if I used the constructor(props) I get undefined value for props I tried also componentDidMount but I get undefined value for probs. Is there a way to dispatch the action using react components ?
I get the correct data from state in the second render.
import React, { Component } from 'react' import {connect} from 'react-redux' import { getUserPosts } from '../../actions' class UserPosts extends Component { //UNSAFE_componentWillMount() { //} constructor(props) { super(props); console.log(props); } componentDidUpdate() { //this.props.dispatch(getUserPosts(this.props.user_reducer.login?.user._id)); } showUserPosts = (user) => ( Array.isArray(user.userPosts) ? user.userPosts.map((item, i) => ( <tr key={i}> <td>{i}</td> <td>author</td> <td>date</td> </tr> )) : null ) render() { let user = this.props.user_reducer; //console.log(user.userPosts); return ( <div> <div className="user_posts"> <h4>Your reviews:</h4> <table> <thead> <tr> <th>Name</th> <th>Author</th> <th>Date</th> </tr> </thead> <tbody> {this.showUserPosts(user)} </tbody> </table> </div> </div> ) } } function mapStateToProps(state) { //console.log(state); return { user_reducer: state.user_reducer } } export default connect(mapStateToProps)(UserPosts) action:
export function getUserPosts(userId) { const req = axios.get(`/api/user_posts?user=${userId}`) .then(res => res.data); return { type: 'GET_USER_POSTS', payload: req } } reducer:
export default function(state = {}, action) { switch(action.type) { case 'USER_LOGIN': return {...state, login: action.payload} case 'USER_AUTH': return {...state, login: action.payload} case 'GET_USER_POSTS': return {...state, userPosts: action.payload} default: return state; } }
_idfrom the url params for example? The_idproperty you are is nested very deeply. It may not be available until after the component has mounted.