|
2 | 2 | "@patternfly/pfe-tools": minor |
3 | 3 | "@patternfly/elements": minor |
4 | 4 | --- |
5 | | -**React**: automatically generate react wrapper components |
| 5 | +PatternFly elements are now available wrapped in React components. While it was |
| 6 | +always possible to use PatternFly elements (or any other custom elements) in |
| 7 | +React apps, this release makes it easier to integrate them into React without |
| 8 | +the need for cumbersome workarounds to React's [poor HTML and DOM support][cee]. |
| 9 | + |
| 10 | +Before: |
| 11 | + |
| 12 | +```jsx |
| 13 | +import { useEffect, useState, useRef } from 'react'; |
| 14 | +import '@patternfly/elements/pf-switch/pf-switch.js' |
| 15 | + |
| 16 | +function App () { |
| 17 | + const [checked, setChecked] = useState(false); |
| 18 | + const switchRef = useRef(null); |
| 19 | + useEffect(() => { |
| 20 | + switchRef.current.checked = checked; |
| 21 | + }, [switchRef.current, checked]); |
| 22 | + useEffect(() => { |
| 23 | + switchRef.current.addEventListener('change', () => |
| 24 | + setChecked(switchRef.current.checked)); |
| 25 | + }, [switchRef.current]); |
| 26 | + return ( |
| 27 | + <> |
| 28 | + <pf-switch ref={switchRef}></pf-switch> |
| 29 | + </> |
| 30 | + ); |
| 31 | +} |
| 32 | +``` |
| 33 | +
|
| 34 | +After: |
| 35 | +
|
| 36 | +```jsx |
| 37 | +import { useState } from 'react'; |
| 38 | +import { Switch } from '@patternfly/elements/react/pf-switch/pf-switch.js'; |
| 39 | + |
| 40 | +function App () { |
| 41 | + const [checked, setChecked] = useState(false); |
| 42 | + return ( |
| 43 | + <> |
| 44 | + <Switch checked={checked} |
| 45 | + onChange={({ target }) => |
| 46 | + setChecked(target.checked)}/> |
| 47 | + </> |
| 48 | + ); |
| 49 | +} |
| 50 | +``` |
| 51 | +
|
| 52 | +[cee]: https://custom-elements-everywhere.com/#react |
0 commit comments