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.

Using with state management

See original GitHub issue

Hello, First, Leva looks really great. I was in the process of rolling my own type of solution before I saw a post on Reddit. Your approach is MUCH cleaner and smarter than my attempt so I’m very intrigued at getting Leva integrated into my app.

My question is about state management/sync, specifically with something like Redux/RTK. Since it seems the each controls state is relatively coupled, what would be the correct way to use values that come from the apps ‘global’ state somewhere, while having the changes get passed down correctly to Leva’s controls?

One approached I’ve tried is a useEffect on the value I want to “watch” from a control and then just dispatch the updated value back to my store. Another is to use the new onChange callback to do the same thing. But I’ll be honest, both of those don’t feel right…

I know I might be missing something obvious and any insight on how to approach this would be great.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dbismutcommented, Mar 30, 2021

Hi! So binding with another state is not something that we’ve considered from the ground up, so you feeling wrong might be normal.

As you’ve noted, leva provides the onChange a callback that doesn’t trigger a render, so that means that you’d have to use your store values to make the component react to changes.

leva also provides a set function (undocumented I believe) that is returned when using the function api.

const [values, set] = useControls(() => inputs)

You could also use that function to react to your store changes and update leva accordingly.

If this doesn’t help or still feels wrong, please provide what you think would be a good api for your usecase and we’ll see what we can do!

0reactions
dbismutcommented, Feb 19, 2022

I’m not sure I understand why anyone would use the set function inside onChange. This is redundant. If you need reactivity with onChange, you can set the transient option to false (this option might be renamed to reactive in the future).

Read more comments on GitHub >

github_iconTop Results From Across the Web

React state management: What is it and why to use it?
React components have a built-in state object. The state is encapsulated data where you store assets that are persistent between component renderings. The...
Read more >
Why State Management is Important For React Apps - Medium
When you have state management in place data actually flows from your app to state and vice versa. You know exactly where your...
Read more >
State Management Battle in React 2021: Hooks, Redux, and ...
State management is simply a way to engender communication and sharing of data across components. It creates a concrete data structure to ...
Read more >
How to Manage State in React - freeCodeCamp
React state management is a process for managing the data that React components need in order to render themselves.
Read more >
List of state management approaches - Flutter documentation
A list of different approaches to managing state. ... Simple app state management, the previous page in this section; Provider package ...
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