The following component includes an useEffect
that should run on mount and then cleanup the effect:
const Group = ({
children,
accessor,
disabled = false,
}: {
children: React.ReactNode
accessor: Accessor
disabled?: boolean
}) => {
const dispatch = useEditorDispatch()
React.useEffect(() => {
if (accessor.length === 1 && field === 'audienceExtension') return
const attribute = `-${field}`
console.log(`on mount ${accessor}`)
dispatch({
type: 'setError',
payload: {
message: 'is empty',
attribute,
accessor,
},
})
return () => {
console.log(`on unmount ${accessor}`)
dispatch({
type: 'setError',
payload: { message: null, attribute, accessor },
})
}
}, [])
The component tree is build by including Group
s inside Group
s. When a Group
mounts I correctly see the accessor
value (incremental) but whenever a Group
unmounts the console logs the latest accessor
value so not the one related to the unmounted Group
. I was expecting the returned closure to capture the props of each Group
and then run the dispatch
with the correct values but that's not the case.