raveclassic / frp-ts

Functional reactive values-over-time
MIT License
80 stars 8 forks source link

usePropertyFromProps inconsistency #79

Open prodderman opened 12 months ago

prodderman commented 12 months ago

Hi @raveclassic!

There is a problem with usePropertyFromProps - the atom changes after the render and at the moment of the react props change prop !== prop$.get():

const Component = ({ prop }) => {
  const prop$ = usePropertyFromProps(prop)

  prop$.get() === prop // false at the moment of the props change 
}

Isn't it better to use the following implementation:

function usePropertyFromProps<Value>(value: Value): Property<Value> {
  const atomRef = useRef(newAtom(value))
  atomRef.current.set(value)
  return atomRef.current
}