I've been struggling with this problem for several weeks now. I'm finally throwing in the towel and asking for help on this because I'm clearly not doing something right. I have a React.js app that is using redux and redux-thunk. I'm simply trying to get my Component Container to initiate the loading of data, but not render until the data comes back from the fetch request. Seems simple enough I know. Here is what I've done:
Container Component
'use strict'; import React, { Component } from 'react'; import { connect } from 'react-redux'; import { fetchActivePlayer } from '../actions/index'; import PlayerDetails from '../components/players/player-detail'; import Spinner from '../components/common/spinner/index'; import store from '../store'; export default class PlayerDetailContainer extends Component { constructor(props) { super(props); } componentWillMount() { this.props.fetchActivePlayer(this.props.params.player_slug) } render() { if (!this.props.activePlayer.activePlayer) { return ( <Spinner text="Loading..." style="fa fa-spinner fa-spin" /> ); } return ( <PlayerDetails player={ this.props.activePlayer.activePlayer } /> ); } } function mapStateToProps(state) { return { activePlayer: state.activePlayer } } export default connect(mapStateToProps, { fetchActivePlayer })(PlayerDetailContainer);
Action Creator
export function fetchActivePlayer(slug) { return (dispatch, getState) => { return axios.get(`${ROOT_URL}/players/${slug}`) .then(response => { dispatch({ type: FETCH_ACTIVE_PLAYER, payload: response }) }) .catch(err => { console.error("Failure: ", err); }); }; }
Store
'use strict'; import React from 'react'; import { browserHistory } from 'react-router'; import { createStore, applyMiddleware } from 'redux'; import { routerMiddleware } from 'react-router-redux'; import thunk from 'redux-thunk'; import promise from 'redux-promise'; import reducers from './components/reducers/index'; const createStoreWithMiddleware = applyMiddleware( thunk, promise, routerMiddleware(browserHistory) ) (createStore); export default createStoreWithMiddleware(reducers);
Routes
export default ( <Route path="/" component={ App }> <IndexRoute component={ HomePage } /> <Route path="players/:player_slug" component={ PlayerContainer } /> <Route path="/:player_slug" component={ PlayerContainer } /> </Route> );
Here are the versions I'm using for everything: react = 0.14.7 react-redux = 4.4.1 redux-thunk = 0.5.3
When I run this, I don't receive any errors but it's clear that my action creator is firing but my component container continues instead of waiting for the creator to finish. Like I said, I'm sure I must be missing something really simple but I can't seem to figure out what that is.
Thank you in advance. Any help would be greatly appreciated.