Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useSetState doesn't work #107

Closed
ilovedesert001 opened this issue Feb 17, 2019 · 12 comments
Closed

useSetState doesn't work #107

ilovedesert001 opened this issue Feb 17, 2019 · 12 comments

Comments

@ilovedesert001
Copy link

since update to react 16.8.2
useSetState doesn't work

https://streamich.github.io/react-use/?selectedKind=State%2FuseSetState&selectedStory=Demo&full=0&addons=1&stories=1&panelRight=0

@ilovedesert001
Copy link
Author

and this works fine

const useSetState = <T extends object> (initialState: T = {} as any): [T, (patch: Function | Partial<T>) => void] => {
  const [state, set] = useState(initialState)
  const setState = (patch) => {
    if (patch instanceof Function) {
      set((prevState) => {
        return Object.assign({}, state, patch(prevState))
      })
    } else {
      const newState = Object.assign({}, state, patch)
      set(newState)
    }
  }
  return [state, setState]
}

streamich added a commit that referenced this issue Feb 17, 2019
@streamich
Copy link
Owner

Thanks for reporting this! Should be fixed now.

streamich pushed a commit that referenced this issue Feb 17, 2019
## [5.3.1](v5.3.0...v5.3.1) (2019-02-17)

### Bug Fixes

* 🐛 fix typings in useOrientation sensor ([afbacac](afbacac))
* 🐛 fix useSetState after React update ([524abe5](524abe5)), closes [#107](#107) [#107](#107)
* useOrientation breaks in some devices ([d89bd54](d89bd54))
@ilovedesert001
Copy link
Author

ilovedesert001 commented Feb 18, 2019

it seems still some bugs ...

maybe the better way

typescript

export const useSetState = <T extends object> (initialState: T = {} as any): [T, (patch: Function | Partial<T>) => void] => {
  const [tick,setTick] = useState(true)

  const [state, set] = useState<T>(initialState);
  const setState = patch => {
    if (patch instanceof Function) set(prevState => Object.assign(prevState, patch(prevState)));
    else set(Object.assign(state, patch));
    setTick(!tick)
  };
  return [state, setState];
}

@streamich streamich reopened this Feb 18, 2019
@ilovedesert001
Copy link
Author

and this works fine

const useSetState = <T extends object> (initialState: T = {} as any): [T, (patch: Function | Partial<T>) => void] => {
  const [state, set] = useState(initialState)
  const setState = (patch) => {
    if (patch instanceof Function) {
      set((prevState) => {
        return Object.assign({}, state, patch(prevState))
      })
    } else {
      const newState = Object.assign({}, state, patch)
      set(newState)
    }
  }
  return [state, setState]
}

This will cause problems when Passing state (Object) to child Components

@streamich
Copy link
Owner

streamich commented Feb 18, 2019

This will cause problems when Passing state (Object) to child Components

@ilovedesert001 Can you give an example? Is it because child components mutate the state object?

@ilovedesert001
Copy link
Author

check out these

what i expect

this may cause problems

@likun7981
Copy link

@ilovedesert001 Why do you use useRef, The returned object will persist for the full lifetime of the component. So a subsequent change is invalid. demo1 is expected, because you assign the patch to state, not create a new object. In fact the state point still has not changed.
image.

So in fact there is no need to use the ref
work fine demo

@ilovedesert001
Copy link
Author

@likun7981
In my case , I don't want to change state point (Or state Object), just need change state property to cause rerender.

@streamich
Q:Is useSetState designed to change the state point (replaced by new state object), or persist state object for the full lifetime of the component ?

@likun7981
Copy link

@ilovedesert001 Every time, the state is a new point(object)

https://codesandbox.io/s/zl1wjo69qx

@streamich
Copy link
Owner

@likun7981 I think this should be ti.

@likun7981
Copy link

@likun7981 I think this should be ti.

These two problems is no relationship

@streamich
Copy link
Owner

@likun7981 OK, sry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants