Programming
October 19, 2023
11 min read
React JS has rapidly grown in popularity and usage over the past few years to become one of the most widely used frameworks for building modern web applications. Created and open sourced by Facebook engineers Jordan Walke and Tom Occhino in 2013, React enables developers to build composable user interfaces by breaking them down into reusable, modular components.
With over 100k stars on GitHub and powering thousands of high-profile apps including Facebook, Instagram, Netflix, and Discord, learning React is an invaluable skill for any front-end developer and expands career opportunities. This interactive course aims to teach you React fundamentals from the ground up and provide practical experience through coding challenges and real-world projects.
By the end, you'll have a solid grasp of core concepts like JSX syntax, components, props, state, lifecycle methods, hooks, routing, and state management. You'll gain the ability to develop complex, reactive user interfaces with composable React components. The knowledge you gain will transfer directly to building mobile apps with React Native as well.
React is an open-source JavaScript library created by Facebook for building fast and interactive user interfaces for web and mobile applications. Here are some of its key characteristics:
For example:
// Header.js
function Header() {
return (
<header>
<h1>My Website</h1>
</header>
);
}
This Header component encapsulates the header markup and can be reused throughout the application.
There are many great reasons to learn React JS:
React is built around several key concepts:
By the end of this course, you should feel comfortable with:
With a strong grasp of these concepts, you'll be prepared to create feature-rich React applications from scratch. Let's start by setting up our React development environment.
Before diving into React code, you need to set up an effective React development environment. This includes choosing a starter project, an editor or IDE, installing Node.js and npm, and identifying helpful React libraries.
The easiest way to start a React project is using Create React App (CRA). It sets up a project with:
The default CRA project gives you a solid folder structure with /src
and /public
folders. Ejecting lets you customize the configuration.
Some popular options for writing React code include:
You'll need Node.js installed to run JavaScript outside the browser and manage dependencies with npm. Consider version managers like nvm
for switching Node versions easily.
Key things to know about npm
and package.json
:
npm install
adds dependencies to node_modules
package.json
stores project config, dependencies, scriptsHere are some useful libraries to consider for a React project:
Equipped with this dev environment, you're ready to learn core React concepts.
Now that you have your dev environment setup, it's time to learn foundational React concepts. This includes an introduction to components, JSX, rendering UI, and handling events and state.
The building blocks of React apps are reusable, modular components that encapsulate UI logic and markup:
For example, we can split a header into its own Header component:
// Header.js
function Header(props) {
return (
<header>
<h1>{props.title}</h1>
</header>
);
}
This Header component receives the title through props and can be reused by passing a title prop to it.
JSX is an XML/HTML-like syntax that gets compiled to JavaScript for rendering UI in React:
className
instead of class
<img />
For example:
<header>
<h1>Hello {name}!</h1>
</header>
This JSX mixes HTML markup with JavaScript code inside the curly braces.
The ReactDOM.render()
method mounts components to the DOM:
<App />
as the first argumentFor example:
ReactDOM.render(
<App />,
document.getElementById('root')
);
This renders the App component into the DOM element with id 'root'.
React components can handle events and manage state:
For example:
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
{count}
</button>
);
}
This component manages the count state to increment and update the UI when clicked.
State allows components to dynamically update in response to events and data changes.
React Hooks introduced a way to add stateful logic and lifecycle methods to function components. Some key hooks include useState, useEffect, useContext, and useReducer. For more complex apps, state management with Redux or MobX is helpful.
The useState hook declares state variables as an array with the variable and a setter function:
const [count, setCount] = useState(0)
setCount
to update the count
valueuseState
callsFor example:
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
function increment() {
setCount(prevCount => prevCount + 1);
}
return (
<div>
<button onClick={increment}>Increment</button>
<span>{count}</span>
</div>
);
}
This component manages the count state variable to increment and display it.
This hook lets you run side effects after render like data fetching:
componentDidMount
For example:
useEffect(() => {
// Fetch data on mount
api.getData();
// Cleanup
return () => {
// Cancel request on unmount
}
}, []);
This fetches data on mount and cleans up on unmount.
The useContext
hook provides access to React context in function components:
React.createContext
and default value<MyContext.Provider value={data}>
to provide contextconst value = useContext(MyContext)
For example:
const ThemeContext = React.createContext('light');
function App() {
return (
<ThemeContext.Provider value="dark">
<Toolbar />
</ThemeContext.Provider>
);
}
function Toolbar() {
const theme = useContext(ThemeContext);
return (
<div className={theme}>
<button>Toggle Theme</button>
</div>
);
}
Any component in the tree can access the theme context.
For complex applications with significant state shared across components, Redux is a popular choice:
connect()
and actionssubscribe
to state changes they needAs you gain more experience with React, you'll encounter powerful patterns like higher-order components, render props, error handling, and suspense APIs.
Higher-order components (HOCs) are advanced techniques in React for logic reuse:
For example:
// withAuth.js
function withAuth(Component) {
return props => {
// Check auth logic
return <Component {...props} />
}
}
This wraps components to handle auth logic.
The render props pattern enables dynamic rendering logic sharing between components:
For example:
<DataFetcher render={data => <h1>{data.title}</h1>} />
DataFetcher fetches data and the render prop displays it.
Error boundaries allow you to gracefully handle React rendering errors:
componentDidCatch
lifecycle methodLazy loading allows code splitting components into separate bundles:
React.lazy
takes a function that imports the componentSuspense
component displays a fallback while loadingFor example:
const OtherComponent = React.lazy(() => import('./OtherComponent'));
function MyComponent() {
return (
<Suspense fallback={<Loader />}>
<OtherComponent />
</Suspense>
);
}
OtherComponent is lazily loaded when rendered.
I hope this overview provides a solid introduction to learning React JS! We covered key concepts like components, props, state, JSX, hooks, routing, and state management that you'll need to build reactive user interfaces.
There is still much more to learn, but you should now feel comfortable diving into React resources like official docs, blogs, courses, and examples. Keep practicing building real-world apps and you'll continue improving as a React developer!
If you're looking for more interactive tutorials on learning React, be sure to check out Learn JavaScript for practical coding lessons and challenges.