I'm using react-hooks. So there is a modal that pops up with a bunch of inputs (components) like text fields, drop-downs, date pickers and etc...
The problem is when some field is being edited, all my form components are being re-rendered and that makes my form very slow. I totally understand why it happens. However, I would like to find a way when I edit some input within my form, all other input fields (components) should stay "frozen" and not re-rendered. Otherwise, working with my form which has at least 20 input fields, would make the work very slow...
Your assistance is appreciated!
You can use Uncontrolled components. It doesn't use setState, so it won't be re-rendered when you type some input value. Then you can send everything when you submit your form.
Try using React.memo which is in some way an equivalent to shouldComponentUpdate
Check React.memo documentation
Related
I have a component with about 16 input fields. The component itself is quite complex. The problem is that every time it updates the state of the form on input change it triggers a re-render. Rendering that component is a bit expensive, there is a short delay noticeable when you type a character inside an input.
What is best practice in such a case?
Maybe I should update the state only when the user submits the form?
if you don't want to use any other library that manages form state,
you can move the form to another component, in this case only the child component that conains your form will rerender on changing inputs values, not the parent component, and pass to it the function that handles submit in props to trigger changes in the parent component when you submit the form
I'm trying to play an animation on dismounting of my component with exit attribute, but it doesn't work despite the presence of !
The input animation works fine but not the output one !
I guess I'm mismanaging the dismounting of my description which is displayed if the props data.show is true, boolean that I change at the click on the project, in short if someone can point me...
codesandbox
Thanks in advance
There are quite a few issues to cover in your code and suggest you first understand what triggers re-rendering in React.
By creating a new uuid key every time you change prop data, you are breaking the ability for React to know which components need to be rendered again so it re-renders them all - so it's always a new instance on AnimatePresence and no way for AnimatePresence to know there has been a change to the key or a change in mounting. Also make use of useCallback hooks when passing in functions to child components to avoid re-renders.
Move the state up to the parent, update the card key to a consistent value between renders, and don't update the entire props data just to expand/collapse.
See the updated sandbox
I suggest that you don’t animate multiple items inline within the same space like this as you will always have the remaining boxes jump a little, but that is another topic.
I create 2 Text Boxes one with hook and the another one with the old style, now in Hook mode when the user input change the textbox value the whole page rendered, but with old style there only the text-box rendered not the whole page, if you have the complex page with many objects like (Data Table, form, buttons, inputs, etc..) the single change on one of these components forced to re-render everything and it decease performance, for example typing in the text-box take time to effect on page and the delay time is more than user typing speed.
the one workaround solution is using the callbacks and useMem but if the page is complex(lots of objects as described, and all of these objects has many properties the callback and useMem is very very complicated to handle single object property.
SandBox Example
This is exactly how react hooks should work. what will cause a rerender in react? two things:
change in props
change in hooks or say in a better way hooks state
in the second case, the useInput state is changing so that causes a rerender and it's absolutely normal behavior. I think using custom hook in that way is not a good practice.
I recommend reading this article for more information on react rendering: https://www.codebeast.dev/usestate-vs-useref-re-render-or-not/
Do you have any experiences with fast react form? I have tried redux-form, antd form. Both are slow. When I am writing inside input fields.
The form what I want to do is quite complicated. It is a dynamic form with many fields. See attached image, that type is select where the selected value is displaying next fields.
Here is example in antd. https://codesandbox.io/s/yjz6r2z2l9
Your form is working slow because of rerendering entire form content on every state change, even single keystroke. React needs some optimizations to avoid that - f.e. shouldComponentUpdate and PureComponent usage.
Try formik. Beside <Field/> it has <FieldArray/> and <FastField/>, probably usabeful for this case.
Remove console.log, use react dev tools.
We've been using redux-form and seeing a noticeable lag when user types in an input field. I'm not really sure if it's a problem with redux-form. This could probably be the way we've structured our components. So we have page which lists some data, say 25 rows and filters for it. On click of a button, we open a modal where we render a redux-form. Now if user types in any of the input field, all the list items in the underlying page also get re-rendered. We're using React.Component for list items. React devtool's "Highlight updates" option highlights list items but when I do a console.log in list item's render method, it's not printing!
Wonder if this is happening because the list item's parent component is also a (redux) connected component and when redux-form's Field updates the store, this also gets re-rendered? How do you use redux-form in such scenario? I don't think having multiple stores is a recommended way.
I'm guessing you have an event listener for when any of the inputs on the form changes, and then you do some fetching/filtering on the underlying list?
Running this when typing quickly could lead to a performance hit, depending on what your event listener does. You could try using something like lodash.debounce to only run your listener after the user has stopped typing for like 200ms?