use-redux-form
With use-redux-form
, any kind of React form components can be possible to use
simply with Redux store object.
- Compatible with any component using
input tag
, select tag
. - For example
<DatePicker {...getFieldProps('createdAt')} />
Install
npm install use-redux-form
Upgrade to v2
const { isValidated, isDisabled, values, errors, handleSubmit, getFieldProps } =
useReduxForm();
useReduxForm({ storePath: 'this-is-store-path' });
useReduxForm('this-is-store-path', {});
useReduxForm({ disable: () => isLoading });
useReduxForm('this-is-store-path', { onDisable: () => isLoading });
useReduxForm('this-is-store-path', { exclude: [] });
useReduxForm('this-is-store-path', { debug: true });
useReduxForm(
'this-is-store-path',
{},
{
},
);
const { value, selected, disabled, name, onChange } = getFieldProps('a.2.b', {
key: () => {},
isRequired: true,
include: [],
exclude: [],
name: 'my-name-is',
});
Example
Example
API Reference
useReduxForm(options)
const {
isValidated,
isDisabled,
values,
errors,
handleSubmit,
getFieldProps
} = useReduxForm('user.form', {
exclude: [],
onDisable: () => isLoading,
validate: (values) => {
const errors = {}
if (!values.username) {
errors.username = 'required!'
}
return errors;
},
transform: ({ name, value }) => {
if (name === 'nil-to-zero') {
return '0'
}
if (name === 'date') {
return new Date(value)
}
return value;
},
onChange: ({ name, value }) => {
if (name === 'A') {
actionA(value)
}
actionB(value)
},
onSubmit: ({ values, errors }) => {
if (!isEmpty(errors)) {
return Object.values(errors).forEach((value) => {
if (value) {
invalidAlert(value);
}
});
}
submit(values);
},
debug: true
}, {
})
return (
<Field {...getFieldProps('username')} />
<Button onClick={handleSubmit}>Submit<Button>
);
getFieldProps(fieldPath, options?)
A function return form field props
const initialState = {
form: {
username: '',
someObj: {
password: '',
},
someArray1: ['hello', 'world'],
someParentState: {
someArray2: ['world', 'hello'],
list: [
{
id: 1,
type: 'A',
name: 'X',
},
{
id: 2,
type: 'B',
name: 'Y',
},
{
id: 3,
type: 'C',
name: 'Z',
},
],
},
},
};
const { value, selected, disabled, name, onChange } = getFieldProps(
'someParentState.list.0.type',
{
name: 'userid',
include: ['name'],
exclude: ['selected'],
find: (fieldState, state) => {
const { type } = givenState.find((item) => item.id === 2);
return type;
},
transform: false,
},
);
License
MIT