React Basics
This totorial is a complete guide core react constructs.
It demonstratens simple implementations of ReactDom
, React Element
, React Component
, props
, in a familiar, real-world component.
Contents
- Online sandboxes
- Put React anywhere!
- Connect a
script
with a single DOM node - Use modern ES Modules
- Import the
react-dom
module - Prepare the root DOM node with
ReactDOM
- Render plain text to React root
- Import
react
and create a React element - Render a React element to React root
- Extract a React component
- Pass custom props to components
- Pass common props to components
- Some common props use “standard DOM” names
- Use objects instead of strings for the
style
prop - Use camelCase for all multi-word common props and properties
- Use object rest syntax to separate custom props from common props
- Quick reference videos
Online sandboxes
- CodeSandbox Used by me (Vim support)
- StackBlitz
Put React anywhere!
React is “just JavaScript”. Add it to any page on the internet.
Start with a simple HTML page like this:
Connect a script
with a single DOM node
React needs a bridge between JavaScript and the DOM. Create a DOM node and query for it in a JavaScript script.
Reference: HTML-only template from React Docs, react.dev. Updated for ES Modules.
Use modern ES Modules
The browser has progressed a lot in a decade.
Specifying type="module"
on script tags to use modern JavaScript imports.
Reference: Applying the module to your HTML, MDN.
Import the react-dom
module
Services like esm.sh allow us to import JavaScript modules from a CDN — no bundling 🥳.
Import the react-dom
module from the esm.sh.
Reference: import API reference, MDN.
Prepare the root DOM node with ReactDOM
React needs to set up event handlers the root DOM node.
Prepare the root DOM node with ReactDOM.createRoot()
.
Reference: React.createRoot API reference, react.dev.
Render plain text to React root
At this point we can render plain text to the React root. Render “Hello ReactDOM” to the React root.
Reference: render API reference, react.dev.
Import react
and create a React element
Next we need React to create React elements and components.
Import react
and create a React element with React.createElement()
.
Reference: createElement API reference, react.dev.
Render a React element to React root
With a React element, we can render it to the React root.
Replace the rendered "Hello ReactDOM"
text with our new React element.
Reference: What is a React element, exactly?, react.dev.
Extract a React component
React components are just functions that return React elements. Declare a new component and use it to create our React element.
Reference: Your first component, react.dev.
Pass custom props to components
Static components, like static functions, are not useful.
Pass a name
prop to the React component and interpolate its value into the React element.
Reference: Passing props to a component, react.dev.
Pass common props to components
In addition to custom props, React components accept common props.
Pass an id
prop to the React component and apply it to the React element.
Reference: Common component props, react.dev.
Some common props use “standard DOM” names
Not all common props use common HTML attributes.
Apply the class
HTML attribute to React elements using the DOM property name className
.
Reference: Element: className property, MDN.
Use objects instead of strings for the style
prop
Unlike HTML, the style
prop requires an object instead of a string.
Apply the style
prop to React elements using an object with camelCased CSS properties.
Reference: Special component props, react.dev.
Use camelCase for all multi-word common props and properties
React does not use attribute style event names.
Add a click handler using the camelCased onClick
prop and a function.
Reference: Common component props, react.dev.
Use object rest syntax to separate custom props from common props
Writing props
before using every value is tedious.
Use object rest syntax to create variables for custom props and collect the rest into a new object.
Reference: Object rest spread, v8.dev.