-
I'm trying to do some scaling experiments with the Supabase example, changing Currently the entire query re-runs (and everything re-renders) if I leave out import { useState } from "react"
import Todos from "../../models/Todos"
import useReactivity from "../../utils/useReactivity"
function App() {
const [text, setText] = useState("")
return (
<>
<input
type="text"
value={text}
placeholder="Type and press Enter to add a new item …"
onChange={(e) => setText(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
if (text === "") { // return on empty => insert 1000 sample items
console.log("add many")
Todos.insertMany(
Array(1000)
.fill({})
.map(() => ({
text: `test ${Math.random()}`,
completed: false,
}))
)
return
}
Todos.insert({ text, completed: false })
setText("")
}
}}
/>
<List />
</>
)
}
function List() {
console.warn("--- rendering List")
const items = useReactivity(
() => Todos.find({}, { fieldTracking: true }).fetch(),
[]
)
return (
<ul>
{items.map((item) => (
<Item key={item.id} item={item} />
))}
{items.length === 0 && <li className="empty">Empty</li>}
</ul>
)
}
function Item({ item }: { item: any }) {
console.log("--- rendering Item", item.id)
return (
<li className={item.completed ? "completed" : ""}>
<input
type="checkbox"
checked={item.completed}
onChange={() =>
Todos.updateOne(
{ id: item.id },
{ $set: { completed: !item.completed } }
)
}
/>
<p>{item.text}</p>
<button onClick={() => Todos.removeOne({ id: item.id })}>x</button>
</li>
)
}
export default App |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
// Dear react, it's day 13234 and everything re-rendered again. Can we reconsider signals, please? (answer: But, but... I am just a library) Jokes/frustration aside, I think this is a react state management issue and not sure it can really be resolved without implementing a third party state management. |
Beta Was this translation helpful? Give feedback.
Okay, answering my own question — by splitting into a 'list' query and a 'details' query, and by using React.memo on the Item (that only gets id as prop), minimal number of re-renders are performed. But of course, this means extra memory use (memo) and extra queries (list/detail split).
If you see a better way, please let me know.