Last active
October 13, 2021 17:13
-
-
Save nathansmith/521059b2221c8070d0b703691e13c9cf to your computer and use it in GitHub Desktop.
React component to conditionally (not) render child components.
This file contains hidden or 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
/* | |
// Used like so… | |
<Render if={isUserAuthenticated}> | |
<p> | |
Super secret UI. | |
</p> | |
</Render> | |
*/ | |
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
const Render = ({ | |
children = null, | |
if: isTruthy = false, | |
}) => { | |
// Assume falsy. | |
let content = null; | |
// Truhty? | |
if (isTruthy) { | |
content = children; | |
} | |
// Expose UI. | |
return <>{content}</>; | |
} | |
Render.propTypes = { | |
children: PropTypes.node, | |
if: PropTypes.any, | |
}; | |
export default Render; |
This file contains hidden or 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
/* | |
// Used like so… | |
<Render if={isUserAuthenticated}> | |
<p> | |
Super secret UI. | |
</p> | |
</Render> | |
*/ | |
import React from 'react'; | |
// ====== | |
// Types. | |
// ====== | |
export interface RenderProps { | |
/* eslint-disable @typescript-eslint/no-explicit-any */ | |
children: any; | |
if: any; | |
/* eslint-enable @typescript-eslint/no-explicit-any */ | |
} | |
// ========== | |
// Component. | |
// ========== | |
const Render: React.FC<RenderProps> = ({ | |
// Props. | |
children = null, | |
if: isTruthy = false, | |
}) => { | |
// Assume falsy. | |
let content = null; | |
// Truhty? | |
if (isTruthy) { | |
content = children; | |
} | |
// Expose UI. | |
return <>{content}</>; | |
}; | |
export default Render; |
This file contains hidden or 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
import React from 'react'; | |
import { render } from 'react-dom'; | |
// Test subject. | |
import Render, { RenderProps } from './Render'; | |
// ==================== | |
// Describe `fileName`. | |
// ==================== | |
describe('Render.tsx', () => { | |
// ====================== | |
// Test default scenario. | |
// ====================== | |
test('handles default scenario', () => { | |
// Dummy text. | |
const DUMMY_TEXT = 'DUMMY_TEXT'; | |
// Dummy props. | |
const props: RenderProps = { | |
children: <p>{DUMMY_TEXT}</p>, | |
if: true, | |
}; | |
// Render. | |
const component = <Render {...props} />; | |
const container = document.createElement('div'); | |
render(component, container); | |
// Get elements. | |
const child = container.querySelector('p') as HTMLElement; | |
// Test assertions. | |
expect(child.textContent).toBe(DUMMY_TEXT); | |
}); | |
// ==================== | |
// Test falsy scenario. | |
// ==================== | |
test('handles falsy scenario', () => { | |
// Dummy text. | |
const DUMMY_TEXT = 'DUMMY_TEXT'; | |
// Dummy props. | |
const props: RenderProps = { | |
children: <p>{DUMMY_TEXT}</p>, | |
if: false, | |
}; | |
// Render. | |
const component = <Render {...props} />; | |
const container = document.createElement('div'); | |
render(component, container); | |
// Get elements. | |
const child = container.querySelector('p') as HTMLElement; | |
// Test assertions. | |
expect(child).toBe(null); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New hotness:
Old and busted:
Also old and busted:
What we're trying to avoid: