API change for rerender
See original GitHub issueContinuing from #28
Instead of asking the developer to import rerender
and manually wire it with args.element
, you could just pass this to the render callback as the third argument, so the developer doesn’t need to know about args.element
(all they care about is causing their component to rerender).
So the render callback would look like:
...
return {
render(props, args, rerender) { // rerender is already primed with args.element so the dev doesn't need to wire it themselves
...
rerender() // <- didn't need to pass args.element
So to achieve that you just need to wrap the rerender function sth like:
// Get a new element by calling render on existing component.
const newForgoNode = newComponentState.component.render(
forgoElement.props,
newComponentState.args,
() => rerender(newComponentState.args.element)) // this third argument can now be called in userland by simply calling rerender() provided as the third argument of the callback
);
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:12 (10 by maintainers)
Top Results From Across the Web
Rerender React component after API is changed
Either refetch it once you submitted and refresh local state that holds your data and/or do an optimistic update on your local state...
Read more >React re-renders guide: everything, all at once - Developer way
Re-render happens when React needs to update the app with some new data. Usually, this happens as a result of a user interacting...
Read more >How and when to force a React component to re-render
React automatically re-renders components, but what happens when a component is not updating as expected? Find out what you can do here.
Read more >5 Ways to Avoid React Component Re-Renderings
1. Memoization using useMemo() and UseCallback() Hooks. Memoization enables your code to re-render components only if there's a change in the props. With...
Read more >React.Component
This page contains a detailed API reference for the React component class definition. ... The default behavior is to re-render on every state...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I do agree.
I think my initial hesitation (and I suspect Jacob’s) was that:
But in the end, update() with destructuring makes for very clean code; and spares the user from learning about args.element. The exception was worth it.
Still have to figure out how to document this - it makes no sense to throw in two styles of update. But it’s WIP.
This is a good point.
This is also a good point. But knowing how to wield rerender() to force a rerender up the tree (by adjusting the componentIndex) might also be an advanced use case - mostly used by expert users.
I asked a few people who are largely unfamiliar with forgo, and for new users it seems rerender() without args is less confusing. One additional point of confusion (for them) is that args.element is not a DOM element, but a structure which identifies a Forgo element.
Now I agree on two things: