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.

[idea] API can be even simplier

See original GitHub issue

Nice lib! I am surprised how little code is needed to work around the problem with React Context API.

One thing that I found missing is the ability to pass a function receiving current value into the setter (just like when using React.useState).

But then I realised this can be done like so:

setRidgeState(state, fn(getRidgeState(state)))

and it will work as long as useRidgeState(state) is called in the component

This is a bit awkward and redundant though.

Would an API like this make sense?

useRidgeSubscription(state) // re render on change, no return
state.get() // getter for the value
state.set(...) = // setter for the value

Now there is only one way of getting and setting the state without extra imports needed. JS getter/setter could be used instead of normal functions. A loose idea, probably too big of a change anyway.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
RichardLindhoutcommented, May 23, 2020

And we need some custom selector which only re renders if own selection change e.g.

import { cartProductsState } from "../CartState";
const filteredProducts = cartProductsState.selector(state => state.filter(p => p.category === 'nice'))
3reactions
RichardLindhoutcommented, May 23, 2020

I agree it could be simpler ideally if you don’t have to import 2 things I was thinking instead of

import { useRidgeState } from "react-ridge-state";
import { cartProductsState } from "../CartState";
const [cartProducts, setCartProducts] = useRidgeState(cartProductsState);

Something like this

import { cartProductsState } from "../CartState";
const [cartProducts, setCartProducts] = cartProductsState.useState();

Global state would be

import { cartProductsState } from "../CartState";
cartProductsState.set(newState)
cartProductsState.get()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use an API: Just the Basics 2022
APIs are useful for more than accessing information for data-driven decision-making. You can also build programs that run searches on the data ...
Read more >
What is an API? Application programming interfaces ...
An even simpler definition is that an API is the part of a software component that is accessible to other components.
Read more >
5 Golden Rules for Great Web API Design
Web APIs that are cleanly-designed, well-documented, and easy-to-use are rare. Here's how to design a great web API that is much more likely...
Read more >
API Crash Course - Integrate, Build, & Sell an API $$$ (EASY!)
Your browser can 't play this video. ... Come up with an idea 2. Build it 3. ... Building, hosting, and selling APIs...
Read more >
7 ideas for the future of APIs | Google Cloud Blog
From Shadow IT to Strategic IT. APIs have gained a reputation as the new Shadow IT, as developers often build them without alerting...
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