Last active
March 19, 2018 08:18
-
-
Save Rendalf/ef14f2202f95cb5e1a3cbaf138a24523 to your computer and use it in GitHub Desktop.
Preact fails ref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// failed ref | |
const MyShinyComponent = ({ isFooShown }) => ( | |
<div className='wrapper'> | |
{ isFooShown && ( | |
<div className='foo'>Foo</div> | |
)} | |
<div | |
className='bar' | |
ref={ someRefFunc } | |
> | |
Bar | |
</div> | |
</div> | |
) | |
// fix with empty element | |
const MyShinyComponent = ({ isFooShown }) => ( | |
<div className='wrapper'> | |
{ isFooShown | |
? <div className='foo'>Foo</div> | |
// returning empty div doesn't cause calling ref with `null` | |
: <div /> | |
} | |
<div | |
className='bar' | |
ref={ someRefFunc } | |
> | |
Bar | |
</div> | |
</div> | |
) | |
// fix with key (best solution) | |
const MyShinyComponent = ({ isFooShown }) => ( | |
<div className='wrapper'> | |
{ isFooShown && ( | |
<div className='foo'>Foo</div> | |
)} | |
<div | |
className='bar' | |
ref={ someRefFunc } | |
key='bar' | |
> | |
Bar | |
</div> | |
</div> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preact calls ref with
null
afterisFooShown
changes fromtrue
tofalse
and doesn't call ref after that.