stowball / dummys-guide-to-redux-and-thunk-react

Tutorial post
https://medium.com/@stowball/a-dummys-guide-to-redux-and-thunk-in-react-d8904a7005d3#.44hlq3vvt
321 stars 146 forks source link

[QUESTION] ¯\_(ツ)_/¯ #2

Closed cjpatoilo closed 6 years ago

cjpatoilo commented 7 years ago

Why not:

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import store from './store/configureStore';
import ItemList from './components/ItemList';

render(
    <Provider store={store}>
        <ItemList />
    </Provider>,
    document.getElementById('app')
)

or

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import configureStore from './store/configureStore';
import ItemList from './components/ItemList';

render(
    <Provider store={configureStore}>
        <ItemList />
    </Provider>,
    document.getElementById('app')
)

Anyway, great tuts! Thanks

stowball commented 7 years ago

configureStore is a function, so it needs to be called first. This would work:

render(
    <Provider store={configureStore()}>
        <ItemList />
    </Provider>,
    document.getElementById('app')
);

I think the reason to have a separate var is so it's cleaner to do all your server-side state configuration and store setup outside, and then always just pass the store={store} regardless of the project you're working on

THEozmic commented 6 years ago

About the configureStore: You seem to be calling dispatch out of the blues, I thought dispatch was the return value of configureStore, but I don't see you assigning it anywhere. Maybe I am way off with my assumption, please clarify.

stowball commented 6 years ago

I'm not sure what you mean. dispatch is part of thunk that needs to be used to call actions from within a thunk https://github.com/gaearon/redux-thunk#motivation

THEozmic commented 6 years ago

Thanks for your reply. I understand what it is now. I have recently (after reading your tuts) been using dispatch without really knowing where is came from, it feels better to know where it's from.

cjpatoilo commented 6 years ago

Thanks @stowball <3