redux-form FieldArray modifies props - reactjs

I am trying to find a lib to use for my forms, to handle validation and stuff, and I come across redux-form which seems to be the most popular one.
There is one thing that troubles me with it though. In FieldArray example https://redux-form.com/7.2.3/examples/fieldarrays/ they modify props to add/remove fields. Isn't that actually anti-pattern which conflicts with functional programming principles?

From the docs: https://redux-form.com/7.3.0/docs/api/fieldarray.md/
fields.push(value:Any) : Function
Adds a value to the end of the array. Returns nothing.
This is not a mutator; it dispatches an action which updates the state in Redux, which will cause your component to rerender.

Related

Is useEffect inside custom hook considered a bad practice?

I was trying to create a reusable hook which includes 2 other mutation hook from react query which does 2 different operations. I was successfully able to create my custom hook and everything is working as expected. My only question is, while building the hook I had to use a useEffect inside my custom hook. I am just wondering if it is a bad practice to have a useEffect inside a custom hook and do I need to change my approach? Will there be any performance issue because of this? Is there something I should be aware of?
It's a very common thing to do. The official documentation describes a custom hook that uses useEffect.
The only thing you should be aware of is that, as always, your hook isn't supposed to intentionally break hook isolation by maintaining an arbitrary shared state outside the hook itself.
I think the "intentionally breaking hook isolation by maintaining an arbitrary shared state outside the hook itself" refers to what happened to me. I was storing a "variable" in the custom hook (with useState) but I was wanting that variable to be accessible in the outside React component, but sometimes the custom hook state was reset, so that value was also reset, and the React component would have suffered from this.
I think the general idea is to always maintain order, you define state in the outer hook/React component, pass that state into custom hooks, the custom hook can generate other data that can be returned as after some processing steps (like with useEffect) and the data can be used in the outer hook/React component as a "state generated by some initial state" which is persisted ONLY in the outer hook/React component caller.
So, in your case and mine, NEVER initialize & maintain an outer hook/React component caller state in the custom hook or NEVER modify the initial state given by the outer caller, in your custom hook.
Sorry, it might be confussing what I wrote, but this is still not very clear for me too. Hope it is clearer though than the short answer Igor gave you.
Good luck !

how to check if checkbox is checked in react styled components

I'm building a project with react styled components, And I need to check if checkbox is checked.
Now I'm doing it with react hook (usestate).
I would love to know if there is a built-in method that returns true or false
The two ways of handling forms with React are described (and documentation linked) below:
Controlled Components
In HTML, form elements such as , , and typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated with setState().
Uncontrolled Components
To write an uncontrolled component, instead of writing an event handler for every state update, you can use a ref to get form values from the DOM.
And also from the React documentation:
If you’re looking for a complete solution including validation, keeping track of the visited fields, and handling form submission, Formik is one of the popular choices. However, it is built on the same principles of controlled components and managing state β€” so don’t neglect to learn them.

Is there any instruction to migrate from redux-form to react-final-form?

I working on a project that need to upgrade redux-form to react-final-form. I just wonder that is there any documentation for that task.
EDIT: πŸŽ‰πŸŽ‰πŸŽ‰ There is now a Migration Guide!! πŸŽ‰πŸŽ‰πŸŽ‰
I meant to make a migration guide, but it's just so easy that I never found the motivation to do it. But I'll write a little here for all of the people who find this excellent SEO-bait via Google.
Rather than "decorate" your form component with a HOC, you use 🏁 React Final Form's <Form> component inside your form component to give you all of your form state via a render prop. Most of the config stuff from Redux Form maps directly onto props to <Form>, e.g. initialValues, onSubmit, etc.
The <Field> API is nearly identical, but with the added benefit that you can define how your field is rendered inline via a render prop (using a "fat arrow" function as your component prop in Redux Form was forbidden and a common pitfall). 🏁 React Final Form has a few extra bits of field state, like dirtySinceLastSubmit, which can come in handy.
By default, Redux Form would not rerender your entire form on every value change, forcing you to use a getFormValues() selector if you needed them in realtime. React Final Form does rerender on every value change by default because for most small forms, this is just fine. But 🏁 React Final Form allows fine tuning of rerendering via providing a subscription prop to <Form>, specifying exactly which pieces (slices) of form state you want to rerender for. Then, any time you were using a selector in Redux Form, you would use a <FormSpy> component in 🏁 React Final Form, which allows you to subscribe ("select") parts of the form state to rerender for.
As linked in the other answer, this talk explains the difference pretty well. More talks will be given later in 2019.

Reducer Props getting changed in the component

In my React app, I have a redux reducer which sends a List as props to my component.
I copy the prop to local state, and show as drop down. User changes the Dropdown so my local state changes.
On click of cancel , I am calling redux Toastr which triggers a method to reset my state with the original props.list. But for some reason the props.list also changed similar to my state change. With my knowledge i thought props passed to the componeent will not be changed until again i call action creator.
Anyone faced similar issue? or i am doing something wrong
Sorry for not posting the code, which I will prepare a demo if needed. Thanks!
What you are doing is an anti-pattern as you are breaking react's rule of single source of truth. You can't hold state internally in a component that is tied up with your redux state and expect it work smoothly. A similar issue of breaking the single source of truth arise when using solely React without Redux and when you try to pass props as state. In this case there is a new lifecycle hook static getDerivedStateFromProps but even this hook is advised to be used sparsely you can read about it here. So if your intent is to reset state back to it's original value, you can either:
Use static getDerivedStateFromProps (which is reserved mostly for UI)
Use a key prop which will reset you back to your initial state
Use a memoization helper such as memoize-one

Editing a form is not working on react redux

I am new on react. I am working on react application with redux. I have a form (I am using redux-form) by which I can save data or edit data.
my problem is , In edit mode I populate data using componentWillReceiveProps. and populated perfectly, now when I try to clear any field on form its again fill.
componentWillReceiveProps(nextProps){
this.props.dispatch(initialize('NewProject', nextProps.project.project[0]));
}
I would be grateful for any help.
Is there a reason you're not dispatching this action somewhere else, like in componentDidMount? I can't say without seeing more code, but it's possible that whenever you edit your form, React again calls componentWillReceiveProps and overwrites whatever you did with the behavior you've given your component.
Per the React documentation:
Note that React may call this method even if the props have not changed, so make sure to compare the current and next values if you only want to handle changes. This may occur when the parent component causes your component to re-render.
It may be a good idea for you to move your dispatch to a more predictable event if possible, like componentDidMount. Typically, your render method should be capable of handling different cases if a component has multiple possible states. You could also create an edit and save version of your component, and render one or the other based on the props you receive. The best way to "populate" data, as you put it, is to define propTypes for your component, and then use your props to insert that data into elements in the render method.

Resources