question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Initial store data is components is always `null`, then loads + re-renders second time to a correct value automatically

See original GitHub issue

This isn’t the end of the world, as I can currently wrap every single component and every store value in an if check to see if the data has actually been loaded, but what seems to be happening is that all components mount and render twice – once with an empty store and then one with the “real” value.

Has anyone seen/heard of this behavior before and have any idea what might be behind it/how to debug? Thank you =)

image

const Table = () => {
	const db = useStoreState(state => state.db)
	const tables = useStoreState(state => state.tables.entries)
	const { name } = useParams()

	console.log("[TABLE COMPONENT]")
	console.log("DB:", db)
	console.log("TABLES:", tables)

	if (!tables || !db) return null

	const table = tables.find(it => it.table_name == name)
	const columns = table?.columns
		.map(it => it.column_name)
		.map(name => ({ Header: name, accessor: name }))

	const req = useQuery(`/table/${name}`, () =>
		db.selectRows(makeRowSelectArgs(table, { limit: 100 }))
	)

	switch (req.status) {
		case "loading":
			return <h1>Loading Tables...</h1>
		case "error":
			return <h1>Error fetching tables: {req.error.message}</h1>
		case "success":
			return <TableComp columns={columns} data={req.data} />
	}
}

Without if (!tables || !db) return null (or equivalent on every component) this happens:

image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
ctrlplusbcommented, Oct 7, 2020

I’ll let you know when the patches are released for test.

1reaction
aalpgiraycommented, Oct 15, 2020

We are on it. But of course, there are breaking changes. Hope we can do it soon. I’ll let you know. Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my React component is rendering twice?
Everything seems to be working fine except it renders twice... The first one renders the phone number and zero points. The second time...
Read more >
React Hooks cheat sheet: Best practices with examples
Declaring a state variable is as simple as calling useState with some initial state value, like so: useState(initialStateValue) . const ...
Read more >
Preventing infinite re-renders when using useEffect and ...
Your first render runs, and because data is falsey, render returns null and kicks off useEffect · useEffect runs, fetching your data, and...
Read more >
Update on Async Rendering – React Blog
The new static getDerivedStateFromProps lifecycle is invoked after a component is instantiated as well as before it is re-rendered. It can ...
Read more >
5 Ways to Avoid React Component Re-Renderings
React components have evolved a long way from their inception. ... The above example has a toggle that re-renders the component each time...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found