-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
InitialValues from state with state load during the rendering #1473
Comments
Just guessing, but I needed to set enableReinitialize to true:
Another option would be to use https://github.com/makeomatic/redux-connect instead of implementing componentWillMount. I do this on a wrapper "page" component - see #1407 (comment) |
Why not just preventing rendering until you received the backend data? /* MyFormContainer.js */
// render method
render() {
if (this.props.loadInProgress === true) { // <-- Use a state variable to know when the load is done or not
return false; // <-- Prevent rendering
} else {
return (
<MyForm onSubmit={this.props.myFormHandler} initialValues={this.props.initialData}/> // <-- MyForm is the redux-form
);
}
}
function mapStateToProps(state) {
return {
loadInProgress: state.categoryRulesForm.loadInProgress, // <-- Pass the variable from state to props
// Other mapping
}
}
componentWillMount() {
// Asynchronously load the initial data
this.props.loadInitialData(this.props.loadParameter);
}
/* MyFormAction.js */
// loadRequested and loadReceived are classic actions
export function loadInitialData(parameter) {
// Async function (thunk)
return function(dispatch) {
dispatch(loadRequested(parameter));
return fetch(`http://example/${parameter}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
})
.then(response => response.json())
.then(json => dispatch(loadReceived(json)));
}
}
/* MyFormReducer.js */
const initialState = {
loadInProgress: true
}
export default function loadInitialData(state = initialState, action) {
switch (action.type) {
case ActionTypes.LOAD_REQUESTED:
return assign({}, state, {
loadInProgress: true
});
case ActionTypes.LOAD_RECEIVED:
return assign({}, state, {
loadInProgress: false
});
default:
return state;
}
} |
Thank you, I'm going to try your solutions ! @sir4ur0n : But in your example does the form will not be initialized during the redux-form decoration in my main container ? Trying your solution there already a first initialization in my action stack before the blocking rendering. |
Sorry that i'm bumping this issue but i wanted to further ask about this.
I dont think this is the best flow and it has to be a mistake here... anyone cares to pitch in? |
Hello, I'm encountering the same issue. Even with the loadInProgress. In fact what Happened is that : After we click cancel for example so RouterDidChange and redux-form-DESTROY. Then we go again to the form, so FETCH_VERSIONS, redux-form/INITIALIZE, and redux-form/DESTROY is called before FETCH_VERSIONS_SUCCESS. I tried with enableReinitialize with no success also. But after the destroy the form is not re-created |
I've done quite a bit of reading about this. What i did on my end was the same as offered in the comment. export default connect((state, ownProps) => {
const wantedObjectId = ownProps.params.objectId;
const actualObjectId = state.objectToEdit.id;
return {
isFetching: state.isFetching || wantedObjectId !== actualObjectId
};
})(MyComponent); So if the isFetching is not yet set because the action did not happen yet, i check what am i supposed to edit against the route. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I have some trouble trying to initialize a redux-form decorated form with some data fetched from a backend (async with redux-thunk) and stored in redux state.
The fact is I need to perform the fetch when a user display the page where the form is and populate the initialValues of the form with the new state values.
My redux-form is connected to the state like it is explained in your example, and I fetch data with an action triggered on the componentWillMount() method.
But during the first render, the redux-form/INITIALIZE action occurs, setting the initialValues with empty data and my action fetching real data and storing them into the state come after. But there is no refresh on the displayed form.
Do you have a specific pattern to work with this case ?
How does it works in your exemple to be able to reload initialValues (and pristine state) when clicking on Load Account button ?
PS : I'm using redux-form 6.0.0.rc4
The text was updated successfully, but these errors were encountered: