Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Portal sample code of React TypeScript Cheatsheet seems broken

I'm using a pretty strict version of TS and ESLint.

I ripped this portal from the docs here: https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/portals/

I modified the code to the below:

import React, { useEffect, useRef, ReactNode } from 'react'
import { createPortal } from 'react-dom'

interface Props {
  children?: ReactNode
  portalId: string
}

export const Portal: React.FC<Props> = ({ children, portalId }) => {
  const el = useRef(document.createElement('div'))

  useEffect(() => {
    const portalRoot = document.querySelector(`#${portalId}`) as HTMLElement

    const current = el.current

    portalRoot.appendChild(current)
    return () => void portalRoot?.removeChild(current) // error thrown here?
  }, [portalId])

  return createPortal(children, el.current)
}

Error below:

Expected 'undefined' and instead saw 'void'

When I remove the void (can't use undefined, otherwise rest of code is unreachable), as such:

import React, { useEffect, useRef, ReactNode } from 'react'
import { createPortal } from 'react-dom'

interface Props {
  children?: ReactNode
  portalId: string
}

export const Portal: React.FC<Props> = ({ children, portalId }) => {
  const el = useRef(document.createElement('div'))

  useEffect(() => {
    const portalRoot = document.querySelector(`#${portalId}`) as HTMLElement

    const current = el.current

    portalRoot.appendChild(current)
    return () => portalRoot?.removeChild(current) // how to fix?
  }, [portalId])

  return createPortal(children, el.current)
}

This results in another error:

Argument of type '() => () => HTMLDivElement' is not assignable to parameter of type 'EffectCallback'.
  Type '() => HTMLDivElement' is not assignable to type 'void | Destructor'.
    Type '() => HTMLDivElement' is not assignable to type 'Destructor'.
      Type 'HTMLDivElement' is not assignable to type 'void | { [UNDEFINED_VOID_ONLY]: never; }'

How to fix?

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It just means that the "Destructor" (i.e. the clean-up function that you return from the useEffect) should return nothing.

But since you use an arrow function short form, its result is automatically returned. Simply wrap it with curly braces for example to prevent the automatic return:

return () => {
  portalRoot?.removeChild(current)
}
almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!