Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Calls to callbacks created via useFacetCallback can get outdated values #118

@xaviervia

Description

@xaviervia

In a scenario where a function that is listening on a facet is calling another function created with useFacetCallback, the value of the same facet inside the second callback might be outdated.

For example:

const facet = createFacet({ initialValue: 'old value' })

function Comp() {
  // Since this is run on useLayoutEffect, it will
  // add the 
  const callback = useFacetCallback((value) => value, [], [facet])

  // Since this is run on render, it will add the
  // `.observe` subscription first
  useMemo(() => {
    facet.observe((value) => {
      console.log('here be new value: ', value)
      console.log('...and here is still old value: ', callback())
    })
  })
  
  return null
}

facet.set('new value')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions