Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation #95

Open
adam-26 opened this issue Dec 9, 2016 · 4 comments
Open

Add documentation #95

adam-26 opened this issue Dec 9, 2016 · 4 comments

Comments

@adam-26
Copy link

adam-26 commented Dec 9, 2016

Hi,

As a reference for others, I think it would be helpful to add the following example to the readme.

// ./containers/User.js
import { asyncConnect } from 'redux-connect'
import { loadUserSession } from '../actionCreators/session'
import User from '../components/User'

const mapStateToProps = (state, ownProps) => {
	// ... as per redux docs
};

const mapDispatchToProps = (dispatch/*, ownProps*/) => {
	// ... as per redux docs
};

const preloadDataActions = [{
        // dispatch actions here required to preload data for the component to render
	promise: ({ params, store: { dispatch }, location }) => {
		return dispatch(loadUserSession(params));
	}
}];

export default asyncConnect(
	preloadDataActions,
	mapStateToProps,
	mapDispatchToProps
)(User);

This approach allows you to isolate all redux related behaviors to a container, without the need to pollute the component code.

IMHO, i think this is a cleaner approach then using the decorator pattern. Helpful if you want to use packaged components without access to the component source.

Want me to submit a PR?

Adam.

@AVVS
Copy link
Member

AVVS commented Dec 9, 2016

Great idea, please do, thanks!

@AVVS
Copy link
Member

AVVS commented Jan 6, 2017

@adam-26 PR is still very welcome. Happy New Year :)

@adam-26
Copy link
Author

adam-26 commented Jan 7, 2017 via email

@vugemsoft
Copy link

vugemsoft commented May 25, 2017

Almost similar to @adam-26 situation, i also have my components setup exactly like this:

// ./containers/User.js
import { asyncConnect } from 'redux-connect'
import User from '../components/User'
import action from '../../actions'

const mapStateToProps = (state, ownProps) => {
	// ... as per redux docs
};

const preloadDataActions = [{
	promise: ({ store: { dispatch } }) => {
		return dispatch(action.auth);
	}
}];

export default asyncConnect(
	preloadDataActions,
	mapStateToProps,
)(User);

But in my router setup, things look like this:

<Route path="somepath" component={Wrapper(User)}>
 /* a lot of children route go here */
</Route>

And the thing is the promise never get fired (state-props mapping works fine). If I strip-off the Wrapper everything works perfect - so what is the correct way or workaround to deal with the Promise action in this situation? Note that I dont really want to touch the Wrapper nor the Route setup.
Tks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants