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.

API change for rerender

See original GitHub issue

Continuing 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jeswincommented, Mar 24, 2021

I do agree.

I think my initial hesitation (and I suspect Jacob’s) was that:

  1. We haven’t done obj.method() in the user facing API till now - either as a class method or as a function that closes over some value. Users can always expect - func(someArg).
  2. PEP 20: There should be one-- and preferably only one --obvious way to do it.
  3. Your suggestion of having the 3rd param (a rerender func closed over args) also had merit; it avoids the x.func() syntax which we have now on args.update(). But the problem with that was - a) How do we know in these early days whether it deserves to be the third param? b) If something else came up, this sets up precedent for fourth param. So it’s too early to decide.

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.

1reaction
jeswincommented, Mar 23, 2021

Again, on the other hand again, I don’t want rerender to be the name of the property and the import from forgo. This is asking for your users to file stupid issues calling the wrong rerender.

This is a good point.

It’s really a toss up for me, but the power of how forgo can force rerenders up and down the tree, not just your own component subtree is one of the things that drew me in. This is kind of masking that for your general user and I do not think it makes it any better.

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:

  1. Of course, we’ll retain the rerender(args.element). That’s set in stone.
  2. The new property must not be called rerender()
Read more comments on GitHub >

github_iconTop 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 >

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