I got one form who is used to Create, Read, Update and Delete. I created 3 components with the same form but I pass them different props. I got CreateForm.js, ViewForm.js (readonly with the delete button) and UpdateForm.js.
I used to work with PHP, so I always did these in one form.
I use React and Redux to manage the store.
When I'm in the CreateForm component, I pass to my sub-components this props createForm={true}
to not fill the inputs with a value and don't disable them. In my ViewForm component, I pass this props readonly="readonly"
.
And I got another problem with a textarea who is filled with a value and is not updatable. React textarea with value is readonly but need to be updated
What's the best structure to have only one component which handles these different states of the form?
Do you have any advice, tutorials, videos, demos to share?
UPDATE: its 2018 and I'll only ever use Formik (or Formik-like libraries)
There is also react-redux-form (step-by-step), which seems exchange some of redux-form's javascript (& boilerplate) with markup declaration. It looks good, but I've not used it yet.
A cut and paste from the readme:
./components/my-form-component.js
Edit: Comparison
The react-redux-form docs provide a comparison vs redux-form:
https://davidkpiano.github.io/react-redux-form/docs/guides/compare-redux-form.html
I found the Redux Form package. It does a really good job!
So, you can use Redux with React-Redux.
First you have to create a form component (obviously):
After this, you connect the component which handles the form:
And add the redux-form reducer in your combined reducers:
And the validator module looks like this:
After the form is completed, when you want to fill all the fields with some values, you can use the
initialize
function:Another way to populate the forms is to set the initialValues.
If you got any other way to handle this, just leave a message! Thank you.
Just another thing for those who want to create fully controlled form component without using oversized library.
ReduxFormHelper - a small ES6 class, less than 100 lines:
It doesn't do all the work for you. However it facilitates creation, validation and handling of a controlled form component. You may just copy & paste the above code into your project or instead, include the respective library -
redux-form-helper
(plug!).How to use
The first step is add specific data to Redux state which will represent the state of our form. These data will include current field values as well as set of error flags for each field in the form.
The form state may be added to an existing reducer or defined in a separate reducer.
Furthermore it's necessary to define specific action initiating update of the form state as well as respective action creator.
Action example:
Reducer example:
The second and final step is create a container component for our form and connect it with respective part of Redux state and actions.
Also we need to define a form model specifying validation of form fields. Now we instantiate
ReduxFormHelper
object as a member of the component and pass there our form model and a callback dispatching update of the form state.Then in the component's
render()
method we have to bind each field'sonChange
and the form'sonSubmit
events withprocessField()
andprocessForm()
methods respectively as well as display error blocks for each field depending on the form error flags in the state.The example below uses CSS from Twitter Bootstrap framework.
Container Component example:
Demo
For those who doesn't care about an enormous library for handling form related issues, I would recommend redux-form-utils.
It can generate value and change handlers for your form controls, generate reducers of the form, handy action creators to clear certain(or all) fields, etc.
All you need to do is assemble them in your code.
By using
redux-form-utils
, you end up with form manipulation like following:However, this library only solves problem
C
andU
, forR
andD
, maybe a more integratedTable
component is to antipate.