JSX in Depth¶
React.createElement
is likedocument.createElement()
<MyComp>
is sugar for ☝️
document.append(element)
is likeReactDOM.render(reactComponent, document.body)
ReactDOM
becauseReact
can be rendered in many environments (likeReact Native
)
React 18: createRoot
¶
no more ReactDOM.render
- import { render } from 'react-dom'
+ import { createRoot } from 'react-dom/client'
const container = document.getElementById('app');
- render(<App tab="home" />, container);
+ const root = createRoot(container);
+ // createRoot(container!) if you use TypeScript
+ root.render(<App tab="home" />);
Portals¶
Child component needs to be rendered outside of the parent component
- Modals
- Tooltips
- Loaders
To ensure the z-index is above everything else
const modalRoot = document.getElementById("modal-root");
const Modal = ({ children }) => ReactDOM.createPortal(children, modalRoot);
Parent div onClick will still work!
onClick still bubbles up as if the modal were a child of the parent
What JSX is converted into¶
syntactic sugar for
React.createElement(
MyButton, // component name
{ color: "blue", shadowSize: 2 }, // props
"Click Me" // children
);
Dot notation¶
It can be an object of Components
const MyComponents = {
DatePicker: function DatePicker(props) {
return <div>Imagine a {props.color} datepicker here.</div>;
},
};
function BlueDatePicker() {
return <MyComponents.DatePicker color="blue" />;
}
or a class Component???
Last update:
2023-04-24