Comments (17)
@prepare(props => action) where action may be async action?
from react-redux.
I'm not sure you'd really need boundActions there. If you give it an action creator, the HOC can take care of dispatching itself.
from react-redux.
Uhm.... I think you need bound actions there because you might want to call multiple (async or not) actions. example
(props, boundActions) => {
boundActions.getUserProfile(props.userId);
boundActions.resetFormX()
}
How can the HoC do this with unbound actions?
I'm not 100% sure if this would add confusion/fuel bad practices among newcomers, but i think that most people would have to use lifecycle events to achieve this, so why not facilitate it into the connector itself, other flux framework offer routeActions (which are pretty much this) and it's a common enough topic of questioning on slack
from react-redux.
This also could possibly facilitate the promise collection needed for server side initial rendering
from react-redux.
I'd like to see this as a separate package in userland first. When its API is nailed, we can consider including it.
from react-redux.
@dzannotti Is it possible to call an action from within another action right now? If it were possible, this might be better done from an action.
This also reminds me of Cerebral's signals, which stack up actions.
from react-redux.
Yes, you can do this with redux-thunk or similar middleware.
// assuming redux-thunk
function prepareComponent(props) {
return function (dispatch) {
dispatch(getUserProfile(props.userId));
dispatch(resetFormX());
}
}
@prepare(prepareComponent)
...
The only problem is we want to only do this when specific prop changes.
from react-redux.
Interesting, I didn't think about that, recalling it on props change should be trivial, knowing if it's needed or not might not be
from react-redux.
That's why I'm not sure there's a general nice solution here. Different projects may solve this differently.
from react-redux.
Interesting pattern: passing custom function instead of action creator as second parameter:
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { fetchUser, fetchRepos } from '../actions';
import User from '../components/User';
class UserPage extends Component {
componentWillMount() {
this.props.fetchData(this.props.userLogin);
}
componentWillReceiveProps(nextProps) {
if (nextProps.userLogin !== this.props.userLogin) {
this.props.fetchData(nextProps.userLogin);
}
}
render() {
const { user } = this.props;
if (!user) {
return <h1>Loading...</h1>;
}
return (
<div>
<User {...user} />
</div>
);
}
}
UserPage.propTypes = {
user: PropTypes.object,
userLogin: PropTypes.string.isRequired,
fetchData: PropTypes.func.isRequired
};
function mapStateToProps(state) {
return {
users: state.database.users
};
}
function mergeProps(stateProps, dispatchProps, ownProps) {
const { users } = stateProps;
const { userLogin } = ownProps.params;
return Object.assign({}, dispatchProps, {
user: users[userLogin],
userLogin
});
}
function fetchData(userLogin) {
return dispatch => Promise.all([
dispatch(fetchUser(userLogin)),
dispatch(fetchRepos(userLogin))
]);
}
export default connect(
mapStateToProps,
{ fetchData },
mergeProps
)(UserPage);
from react-redux.
That's pretty much the pattern i was looking for, but i need bind action + fetchData
from react-redux.
Why do you need it to be bound there?
function fetchData(userLogin) {
return dispatch => {
return Promise.all([
dispatch(fetchUser(userLogin)),
dispatch(fetchRepos(userLogin))
]).then(() => dispatch(resetForm());
};
}
from react-redux.
Oh no, i don't, i just need boundActions to be passed down to the component, that's all
from react-redux.
Yeah re-reading the pattern is slightly off from what i normally use/need.
connect((state) => mereStateToProps, actionCreators, (props, dispatch) => {
// execute this on mount/props change
dispatch(fetchData());
})(MyComponent);
this would allow MyComponent to remain pure while still having route based actions
from react-redux.
Take a look at this (decorator) function. https://github.com/broucz/threads-redux/blob/master/client/decorators/onUpdate.js
The callback is only invoked if certain parameters change.
from react-redux.
While I'm happy to see this in userland, I don't think it belongs in react-redux
.
from react-redux.
Here's another example of such HOC from redux-react-router-async-example:
import React, { PropTypes } from 'react'
import shallowEqual from 'react-redux/lib/utils/shallowEqual'
function mapParams (paramKeys, params) {
return paramKeys.reduce((acc, key) => {
return Object.assign({}, acc, { [key]: params[key] })
}, {})
}
export default function fetchOnUpdate (paramKeys, fn) {
return DecoratedComponent =>
class FetchOnUpdateDecorator extends React.Component {
static propTypes = {
actions: PropTypes.object
}
componentWillMount () {
fn(mapParams(paramKeys, this.props.params), this.props.actions)
}
componentDidUpdate (prevProps) {
const params = mapParams(paramKeys, this.props.params)
const prevParams = mapParams(paramKeys, prevProps.params)
if (!shallowEqual(params, prevParams))
fn(params, this.props.actions)
}
render () {
return (
<DecoratedComponent {...this.props} />
)
}
}
}
@fetchOnUpdate([ 'username', 'repo' ], (params, actions) => {
const { username, repo } = params
actions.fetchRepo({ username, repo })
actions.fetchRepoStargazers({ username, repo })
})
export default class Repo extends React.Component {
...
}
from react-redux.
Related Issues (20)
- Add a debug check to `<Provider>` to throw a more detailed error when used in Next HOT 7
- Could not find react-redux context value; please ensure the component is wrapped in a <Provider> HOT 4
- Performance of useSyncExternalStore and React transitions HOT 5
- Disconnect from redux HOT 20
- Check why we no longer bail out of subscription checks when `store.getState()` hasn't changed HOT 1
- Rename `noopCheck` to `identityFunctionCheck` HOT 1
- Color visibility of some note HOT 5
- Unable to resolve module react-dom HOT 10
- Jest: Module import syntax error HOT 9
- Unable to resolve peer dependency react-native on [email protected] HOT 1
- TypeError: dispatcher.useSyncExternalStore is not a function HOT 1
- where development or production mode should be specified? HOT 7
- After some time useSelector stops doing re-renders HOT 8
- Provider properties for dev checks to be passed explicitly to new devModeChecks property HOT 7
- Getting Syntax error for react-redux when building project HOT 4
- Connect not working with detox envinronment HOT 1
- Getting issue in middleware of configureStore when update redux-toolkit to 2.0.1 HOT 1
- useDispatch() causing unexpected navigate back to previous screen HOT 3
- Single global store and two Provider occurs an error for microfrontend app HOT 1
- Test fail with provider in react-native HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-redux.