Skip to content

useOnceCreated

Александр edited this page Jan 17, 2023 · 1 revision
export function useOnceCreated<T>(factory: () => T): T

Calls the passed function once. Returns its cached value.

import { afc } from 'react-afc'
import { useOnceCreated, useOnMount } from 'react-afc/compatible'

function withHardCalc() {
  const bigValue = useOnceCreated(() => {/* calculations */})
  
  useOnMount(() => {
    console.log(bigValue)
  })
}

const AFCComponent = afc(props => {
  withHardCalc()
  return () => (
    <p>content</p>
  )
})

function CommonComponent(props) {
  // It will work the same way as in the afc component.
  // Will not cause errors.
  withHardCalc()
  return (
    <p>content</p>
  )
}
Clone this wiki locally