Answer the question
In order to leave comments, you need to log in
How to speed up rendering after changing input?
Hello. My project is slowing down due to changing inputs. I, as expected, hung up an event handler so that after each change in value in input, dispatch was called and the new value was transferred to state, and then displayed. How is changing input fields usually optimized in React projects?
I have a number of input fields generated on the page. Because I don't know how many inputs there will be, I don't use useRef(). How important is it to use the useRef() hook for input fields?
Do I need to use the bind() method when passing dispatch via props?
And in general, did I adequately implement the scheme for changing the value in the input, or is it shit code?
I decided not to use Redux, since the project is not very large.
app.js:
const initialState = { text: [] } // это написано вне App.js.
const reducer = (textarea, action) => {
let arr = textarea.text // массив text содержит объекты типа: { id: 0, string: 'string' }
let index = 0
switch (action.type) {
case 'change':
index = arr.findIndex(n => n.id === action.id)
arr[index].string = action.value
return { text: arr }
default:
console.log('Error')
}
}
const App = () => {
const [textarea, dispatch] = useReducer(reducer, initialState)
}
{textarea.text.map(data =>
<Textarea key={data.id} string={data.string} id={data.id} dispatch={dispatch} />
)}
const change = ({ value, id }) => {
props.dispatch({ type: 'change', id: Number(id), value: value })
}
<textarea id={props.id} value={props.string} onChange={(e) => change(e.target)} />
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question