The input was working with no problem, but I needed to move the input code into a function. Now I can only type in one word in the input value. Not sure what the solution would be to fix this issue. I’m also having a similar issue with my sorting function.
here is the link to my code
https://codesandbox.io/s/sweet-murdock-5uqfxs?file=/src/App.js
I created another function for the input, and still no luck.
2
Answers
That is because you define the
MyInput
component inside theApp
component. So in each re-render ofApp
it will create a newMyInput
component so it will lose focus and you need to re-focus the input.Defining the
MyInput
component outside ofApp
so that it is stable will fix the issues. You will have to pass the required values and handlers through props.In general it is a bad pattern to define components inside other components, due to this kind of problems.
Your code is dirty and painful to read.
You have unnecessarily used props almost everywhere. Your app is basically a to-do list for shopping items. The app Creates and Reads the data – the only two functionalities.
Also you have unnecessarily composed your basic app into multiple components within the main App component. Due to this, somehow your
inputValue
state doesn’t exactly map tousername
inside MyInput.Suggestion: Organize your state variables and reduce composition of components.
I am not trying to be rude, but your code is not organized. It is painful to read.