GithubHelp home page GithubHelp logo

usePureState about react-use HOT 7 OPEN

cloudever avatar cloudever commented on April 19, 2024 1
usePureState

from react-use.

Comments (7)

dpikt avatar dpikt commented on April 19, 2024 2

Could this be implemented via a hook that allows a custom comparator function to be used for state updates? It looks like useState already avoids re-renders for shallowly equal updates, we would just need to expand the concept of what an "equal" update is.

// using default useState comparator 
const [ state, setState ] = useDistinctState('a')
setState('A') // will update
...
// using a custom comparator
const [ state, setState ] = useDistinctState('a', 
    (prev, next) => prev.toUpperCase() === next.toUpperCase()
)
setState('A') // won't update
...
// solving issue use case
function usePureState (initialValue) {
   return useDistinctState(initialValue, shallowEqual)
}

(I just came from reading #92 so that's part of my consideration here.)

from react-use.

streamich avatar streamich commented on April 19, 2024 1

react-fast-compare does deep compare, as I understand, not shallow.

from react-use.

streamich avatar streamich commented on April 19, 2024

Do you mean it would not re-render if props didn't change?

from react-use.

revskill10 avatar revskill10 commented on April 19, 2024

Could react-fast-compare help here ?

from react-use.

cloudever avatar cloudever commented on April 19, 2024

I mean that if use useState with object like this.setState, it would be great to skip update using shallow compare of object values like that

function Component() {
  const [state, setState] = usePureState({ a: 1, b: 2 })

  // skip update
  setState({ a: 1 }) // merges automatically
  // updates
  setState({ a: 2})
 
}

from react-use.

streamich avatar streamich commented on April 19, 2024

Yeah, sounds like something useful. I guess, could be easy to do, too, just with just a single loop that shallow-checks all values.

from react-use.

chriswilty avatar chriswilty commented on April 19, 2024

This is how React do shallowEquals:
https://github.com/facebook/react/blob/master/packages/shared/shallowEqual.js

I would hope that usePureState could get included as a built-in hook. Saves us all re-implementing (read copy-pasting) the above comparator.

from react-use.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.