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
Handling form defaults #176
Comments
As I understand, the redux-way to achieve your goal is following:
import {createStore, combineReducers} from 'redux';
import {reducer as formReducer} from 'redux-form';
import defaultValuePlugin from './plugins/default-value';
const reducers = {
// ... your other reducers here ...
form: formReducer.plugin({
formWithDefaultValue: defaultValuePlugin
})
}
const reducer = combineReducers(reducers);
const store = createStore(reducer);
export default function defaultValuePlugin(state, action) {
switch (action.type) {
case 'CAR_LIST_LOADED':
const firstCarId = getFirstCarId(action.data);
return {
...state,
carSelector: {
value: firstCarId,
},
};
default:
return state;
}
}
So, everytime when you need to change form value (to set defaults, to update values, etc.) you dispatch specific action, and handle it in your plugin. |
Yeah, that's an alternative solution to the higher component, but the question is: Should this be really done like this? Should'nt forms defaults stick together with form definition? Also, in your example you listen to car list loaded, it should listen for form initialization (and that's why time ago I requested for actionTypes export ;) and that makes the thinks a bit trickier |
I think it should listen both actions (internal form initialise action and car list load action), because you can't guarantee that car list is loaded before form initialisation :) |
I have same question about default values. In my case it's an account profile form which will have user's current profile values as default values, and user's data is in I was thinking about using UPDATE: actually I found |
@mattiamanzati I just re-read this issue, and I have a question. Already you can use |
I am talking about a set of default values that should be merged with initialValues. |
I don't understand initialValues vs defaultValues. IMHO the only sane behavior is that the initialValues prop directly maps to the default values of fields, and when it changes, the change in defaults is dispatched. It is presumably always safe to change defaults, if there are conflicts possible between set and unset fields that is uu to the programmer to handle… |
I don't understand initialValues vs defaultValues either.. Is there any example showing how to use these properties? Especially for customised components. Additionally, for my customized component, I tried doing initialization in the constructor via
I feel it is wrong cuz it's relaying upon |
Any solution to this? |
@madshargreave Well, there's the Initialize From State example that was more or less a response to this issue. And there is also an active discussion going on in #578 about modifying the decision in #370. |
FYI, |
Would this work: `<InputField value={defaultVal} {...field}/>`? If value is
defined in field it will overwrite the default.
|
Honestly I ended up wrapping the entire form component in a simple
|
ah yes that's best :)
|
Just incase anyone else finds this issue and has issues this is how I solved it: @connect(({ user: { profile: { first, middle, last, email } } }) => {
const fullName = joinNameParts(first, middle, last)
return { initialValues: { fullName, email } }
})
@reduxForm({ form: 'edit-profile' }) Where my form had fields |
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. |
Hi all, thanks again for this awesome library, and thanks for the v3 rewrite, I was waiting for that! :D
I use redux-form since 3 months ago, and the only thing I am not too comfortable with is the way of defaults value is handled. Maybe I am missing something, and I know (and I am actually doing it) this could be solved by an higher component of the form component, but I think that this problem should be addressed by the reduxForm() function.
Here's an example.
I have a custom component which displays a select with options (of cars for example) picked from the state, and by default the form should assume as value the first one available.
Assuming that the cars picker components is a controller component with value and onChange, is almost impossible to make, by default, the first car of the state.cars selected.
Here's my purposal.
Why not provide some sort of mapStateToDefaultFormValue?
The function takes in the state (as connect's mapStateToProps does), the form component props and, as extra that could be usefull, a copy of the current field confing that could be used, for example, to set the default value to all fields.
The return of this defaults, is then merged with initialValues provided.
This way initial values contains only the actual form data stored in the db, and this enables things like
for forms with pickers.The text was updated successfully, but these errors were encountered: