I have a class ListItem which is a list of Item. It got some function like setItem that update an item in the list and addItem that add an item and filterItem that set visible
Item is an object that looks like {name,number,discount,visible}
I have a view ListItemView with a pseudo code like this
export default function ListItemView() {
const [items, setItems] = useState([])
useEffect(() => {
setItems(new ListItem(getItems()))
}, [])
function toggleChange(f,newF){
setItems((current) => current.setItem(f,newF))
}
function filter(search){
setItems(Items.filterItem({search}))
}
return (
<>
<button onClick={() => setItems(items.addItem())}> ADD Item</button>
<input onChange={(e) => filter(e.target.value)} />
{items.items.map((Item) =>
!item.visible && item.visible !== undefined ? null :
<div key={Item.id}>
<ItemLine f={item} toggleChange={toggleChange} />
</div>
)}
</>
)
}
And ItemLine is just a component that display inputs with the datas of my item
So there is my questions :
To update my items, am I forced to use
setItemseverytimes ? Because it's causing the full rerender of the page, that's quite dumb for me because when we are just setting 1 data in one object of an entire list ?My filter function is quick when I'm typing (to filter on my list), but it's pretty slow when I have to reshow every items in my list, with 300 items it would be unusable, how can I change this ?
I have heard of ReactHook form, would it work for me here ?
Thanks for your expertise :)
itemsstate is an array, what is the state updater doing to your state, i.e. what iscurrent.setItem(f,newF)? You are mappingitems.items, so now I'm thinking that youritemsstate isn't actually an array and your mountinguseEffectis mutating the state shape.itemsinitial state it should benew ListItem()but my mountinguseEffectsetitemstonew ListItem(someitems)wheresomeitemsis an array of items that I get from my database.current.setItem(f,newF)will searchfthe olditeminitems.itemsand putnewFwhich is the new item, for exemple a new name. Maybe I should have showListItemclass but it's simply a class withthis.itemsas state