I'm new to React and pretty confused I must say, thus the question. I build the first iteration of my app fetching data inside my root/container components. Here's the container
class HomePage extends React.Component{
constructor(props) {
super(props);
this.state = {user: null};
}
componentWillMount() {
getUserData(xobni_api).then((result) =>{
this.setState({user : result});
});
render(){
return(
{this.state.user &&
<div className="col-md-8 middle-pane">
<Viewer images={this.state.user} />
</div>
}
);
}
}
This is the getUserData function,
export function getUserData(url){
return fetch(url, { credentials : 'include'})
.then((response) => { return response.json();})
.then((data) =>{ return new User(data.public_id, data.yahoo_profile.givenName, data.yahoo_profile.familyName, data.credentials[0].account);});
}
Now I'm in the process of restructuring my app and want to get the manage the data using Redux. Here's my condifgureStore
export default function configureStore(initialState){
return createStore(
rootReducer,
initialState,
applyMiddleware(reduxImmutableStateInvariant())
);
}
And I'm instantiating the Store in my index.js
const store = configureStore(initialState);
And then I need to pass this state into my individual Reducers, like this one.
export default function homePageReducer(state, action){
switch(action.type){
case 'SEND_IMAGE':
//send image
default:
return state;
}
}
I can't seem to find the right way to do this. I would really appreciate if someone can help with a slightly detailed answer as to how I can move the api call out of my component, make the call in the store, set the initial State and then pass it back to the component. Thanks in advance.