toss / suspensive

Manage asynchronous operations, timing, error handling, detecting intersection of elements, and caching easily and declaratively
https://suspensive.org
MIT License
490 stars 48 forks source link

[Question]: Why do we manage usePrevious together inside useIsChanged? #1166

Closed ssi02014 closed 1 month ago

ssi02014 commented 1 month ago

Package Scope

@suspensive/react

Description

I think it's better to split them up, what do you think?

Possible Solution

to-be

// usePrevious.ts
import { useEffect, useRef } from 'react'

export const usePrevious = <TValue>(value: TValue): TValue => {
  const ref = useRef<TValue>(value)

  useEffect(() => {
    ref.current = value
  }, [value])

  return ref.current
}
// useIsChanged.ts
import { usePrevious } from './usePrevious'

export const useIsChanged = (value: unknown) => usePrevious(value) !== value

etc.

No response

manudeli commented 1 month ago

Package Scope

@suspensive/react

Description

I think it's better to split them up, what do you think?

Possible Solution

to-be

// usePrevious.ts
import { useEffect, useRef } from 'react'

export const usePrevious = <TValue>(value: TValue): TValue => {
  const ref = useRef<TValue>(value)

  useEffect(() => {
    ref.current = value
  }, [value])

  return ref.current
}
// useIsChanged.ts
import { usePrevious } from './usePrevious'

export const useIsChanged = (value: unknown) => usePrevious(value) !== value

etc.

No response

Cool, Could I assign you to do this issue? In my opinion, @suspensive/utils could be good place to contain it too.

But usePrevious have no use case in other package, so do this in only @suspensive/react first.

ssi02014 commented 1 month ago

@manudeli Yes! 🤗 I will create a pull request after working on it as soon as possible.