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.

[Question]: What is the most performant way for dynamically adding atoms or focus atoms for a large tree?

See original GitHub issue

Hi folks,

First of all, great work done with Jotai. Good documentation too.

I have a use-case where i am require to maintain a component tree and its styles, which are to be rendered on a page. Each of this component’s style can be modified. At the moment i use a single useState to update the tree whenever a component style is updated. I feel Jotai can help optimise for performance here as this tree can grow quite large.

Example tree:

const componentTree = {
  id: 'root',
  el: 'div',
  props: {
     style: {
       background: 'grey'
     }
  },
  children: [
    {
      id: '1dfsg2',
      el: 'p',
      props: {
        style: {
          fontSize: '16px'
        }
      },
      children: 'Heading'
     },
     {
      id: '38nsd7',
      el: 'div',
      props: {
        style: {
          marginTop: '10px'
        }
      },
      children: [
        {
          id: '32fss2d',
          el: 'p',
          props: {
            style: {
              fontSize: '12px'
            }
          },
          children: 'Sub text'
         }
      ]
     }
  ]
 }

My question is what is the best way to add state to this given we have dynamic tree. I saw an example here which deals with a similar problem using focusAtoms https://github.com/pmndrs/jotai/blob/master/docs/advanced-recipes/large-objects.md

However, in the above tree the only unique field i have is an id, which is auto generated whenever a component is added to the tree. I can probably use that to dynamically create an atom but issue with that approach is, how do i update the whole component tree itself.

Would appreciate some help/suggestions here on how to best use Jotai for my example.

Cheers 😃

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
Aslemammadcommented, May 7, 2021

@dai-shi After 443, I’ll get back to this one, but maybe @PuruVJ can figure this out if he’s interested.

1reaction
dai-shicommented, May 31, 2021

This has been a great discussion. We’ve fixed some bugs. The current recommendation for tree structure is using splitAtom and focusAtom. However, we noticed some use cases should be handled more nicely. We will continue to explore best practices and collect feedbacks.

Closing this now, and for further discussions, please open a new issue/discussion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to generate atoms dynamically in Elixir?
It is generally considered a bad idea to dynamically generate them. However, to answer your question.
Read more >
Jotai vs. Recoil: What are the differences? - LogRocket Blog
Jotai and Recoil both have their benefits. This article compares the state management libraries to help you decide what's best for you.
Read more >
Jotai: Atom-based state management for React
Recoil takes an Atomic approach like a graph that attaches to your React Tree where the state changes flow from the roots of...
Read more >
XSL Transformations (XSLT) Version 3.0 - W3C
An xsl:evaluate instruction allowing evaluation of XPath expressions that are dynamically constructed as strings, or that are read from a source ...
Read more >
Artificial intelligence: A powerful paradigm for scientific research
However, they soon realized that there was still a long way to go ... of AI algorithms to automatically generate DNN has attracted...
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