| 1 |
/** |
| 2 |
* Common functions for loading React components in TablePress JS. |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Views JavaScript |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 2.2.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* WordPress dependencies. |
| 12 |
*/ |
| 13 |
import { StrictMode } from 'react'; |
| 14 |
import { createRoot, render } from 'react-dom'; // eslint-disable-line react/no-deprecated |
| 15 |
|
| 16 |
/** |
| 17 |
* Initializes a React component on the page. |
| 18 |
* |
| 19 |
* @param {string} rootId HTML ID of the root element for the component. |
| 20 |
* @param {Component} component JSX of the component. |
| 21 |
*/ |
| 22 |
export const initializeReactComponent = ( rootId, component ) => { |
| 23 |
if ( process.env.DEVELOP ) { |
| 24 |
component = <StrictMode>{ component }</StrictMode>; |
| 25 |
} |
| 26 |
|
| 27 |
const root = document.getElementById( rootId ); |
| 28 |
if ( root ) { |
| 29 |
// Compatibility check for React 17 and 18. |
| 30 |
if ( 'function' === typeof createRoot ) { |
| 31 |
// React 18 (WP 6.2 and newer): Use createRoot(). |
| 32 |
createRoot( root ).render( component ); |
| 33 |
} else { |
| 34 |
// React 17 (WP 6.1 and older): Use render(). |
| 35 |
render( component, root ); |
| 36 |
} |
| 37 |
} |
| 38 |
}; |
| 39 |
|