Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeScript with React. Property 'style' does not exist on type 'MutableRefObject<null>'

This is a snippet of my code in a component. I’m trying to make a component whose height will change when the prop changes. How to fix the error? Property ‘style’ does not exist on type ‘MutableRefObject’

    const bottom = useRef(null)

    useEffect(() => {
        active ?
        bottom.style.maxHeight = '120px'
        :
        bottom.style.maxHeight = '0px'
    }, [active])

    return (
        <div ref={bottom}></div>
    )

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 const bottom = useRef<HTMLDivElement>(null)
  useEffect(() => {
    if (bottom.current) {
      bottom.current.style.maxHeight = active ? '120px': '0'
    }
  }, [active])
  • You need to specify the element type in your useRef
  • You need to use bottom.current
  • I’m not sure you can make assignments in a ternary
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading