This article serves as a broad overview of React.js. For more details, check out the linked pages.
React.js is a popular JavaScript library for building user interfaces, especially for single-page applications. It's used for handling the view layer in web and mobile apps. React allows you to design simple views for each state in your application, and it will efficiently update and render the components when your data changes. The core aspects of React that every developer should grasp include components, state and props, hooks, routing with React-Router, Context API, and higher order components.
Introduction to React.js
React.js was developed by Facebook to address the need for a dynamic and high performing User Interface(UI). The main feature of React.js is the ability to break down complex UI into simpler components by utilizing the power of JSX. Not only does this make the code more readable and easier to maintain, but it also improves performance through efficient DOM manipulation.
import React from 'react';
import ReactDOM from 'react-dom';
function Hello() {
return <h1>Hello, world!</h1>;
}
ReactDOM.render(
<Hello />,
document.getElementById('root')
);
This is a simple example of a functional component in React. We have a function Hello
that returns a single <h1>
element. The ReactDOM.render
method then takes the Hello
component and renders it to the DOM at the root
div.
Understanding React.js Components
Components are the building blocks of any React application, and a single app usually consists of multiple components. A component is essentially a JavaScript class or function that accepts inputs (called props
) and returns a React element that describes how a section of the UI should appear.
import React from 'react';
functino Welcome() {
return <h1>Welcome, {this.props.name}</h1>;
}
In the above code snippet, Welcome
is a simple React class component that takes in a prop
(in this case, name
) and outputs an <h1>
element. You can use the Welcome
component like any other HTML tag, <Welcome name="John Doe" />
, and it will output <h1>Welcome, John Doe</h1>
.
Grasping React.js State and Props
In React, both state and props are JavaScript objects. While both of them hold information that influences the output of render, they are different in their functionality with respect to component. Props
(short for properties) are a way of passing data from parent to child components, whereas state
is a data structure that allows a component to control its own rendering.
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
}
return (
<div>
<p>You clicked {count} times</p>
<button onClick={handleClick}>
Click me
</button>
</div>
);
}
In this code snippet, we have a simple Counter
component that increments a value every time a button is clicked. The state
is initialized in the constructor and then updated using the setState
method.
Introduction to React Hooks
React Hooks are a new addition in React 16.8 that lets you use state and other React features without writing a class. Hooks are functions that let you "hook into" React state and lifecycle features from function components. You can even write your own, custom hooks.
The state of the component can be manipulated using the useState hook.
import { useState } from "react";
function FavoriteColor() {
const [color, setColor] = useState("red");
return (
<>
<h1>My favorite color is {color}!</h1>
<button
type="button"
onClick={() => setColor("blue")}
>Blue</button>
</>
)
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<FavoriteColor />);
In the above example, we first import the useState
hook from the react
library. Then, in the FavoriteColor
component, we declare a state variable color
, and set its initial value to "red". The useState
hook returns a pair: the current state value and a function that lets you update it. You can call this function (setColor
) from an event handler or somewhere else to update the state. The component will re-render with the new state when the setState
function is called. This example also demonstrates how to read state in the component and how to update it. The useState
hook can be used to keep track of any type of data including strings, numbers, booleans, arrays, and objects.
Styling React components
React.js is a go-to choice for many developers when creating user interfaces. But, what about styling those interfaces? The truth is, styling in React.js is as integral as any other aspect of your application, bringing your UI to life. Let’s explore two of the most popular methods for styling in React.js: CSS-in-JSX and Styled-Components.
CSS-in-JS
In a typical HTML and CSS setup, you might have separate stylesheets. But with React.js, you can inject your CSS right into your JSX. This approach, known as CSS-in-JS, is a powerful tool in creating dynamic, visually appealing components.
const myStyle = {
color: "white",
backgroundColor: "DodgerBlue",
padding: "10px",
fontFamily: "Arial"
};
const MyComponent = () => {
return <h1 style={myStyle}>Hello Style!</h1>;
};
In the above example, the myStyle
object contains CSS properties and values, defined in camelCase notation. This style object is then passed to the style
attribute of the HTML element in the JSX. The resulting h1
tag will have a white color text on a DodgerBlue background, styled with Arial font and a padding of 10px.
Leveraging the Power of Styled-Components
While CSS-in-JSX provides a quick way to style components, styled-components bring an extra level of power and flexibility. Styled-components is a library for React and React Native that allows you to use component-level styles in your application that are written with a mixture of JavaScript and CSS.
import styled from 'styled-components';
const StyledButton = styled.button`
background-color: coral;
color: white;
font-size: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
const MyComponent = () => {
return <StyledButton>Click me</StyledButton>;
};
In this code snippet, we first import the styled
object from the styled-components
library. We then create a StyledButton
component using the styled.button
syntax. The CSS for this component is written within a template literal, enclosed in backticks. The resulting StyledButton
component can be used just like any other React component.
Routing with React-Router
React Router is a standard library system built on top of the React and used to create routing in the React application using React Router Package. It provides the synchronous URL on the browser with data that will be displayed on the web page. It maintains the standard structure and behavior of the web application and is used for developing single page web applications.
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import Home from "./Home";
import About from "./About";
function App() {
return (
<Router>
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</Router>
);
}
In the above example, the BrowserRouter
is used as a <Router>
component. The <Switch>
component is used to render only the first <Route>
or <Redirect>
that matches the location. The <Route>
component is used to define the mapping between the URL path and the component that should be rendered. The <Route path="/about">
means that the About
component will be rendered when the URL path is '/about'.
Context API
React's Context API is a feature for component tree-wide state management. A context in React is a way to share values between components without having to explicitly pass a prop through every level of the tree.
const colors = {
blue: "#03619c",
yellow: "#8c8f03",
red: "#9c0312"
};
export const ColorContext = React.createContext(colors.blue);
import React from 'react';
import { ColorContext } from "./ColorContext";
function App() {
return (
<ColorContext.Provider value={colors}>
<Home />
</ColorContext.Provider>
);
}
return (
<ColorContext.Consumer>
{colors => <div style={colors.blue}>Hello World</div>}
</ColorContext.Consumer>
);
import React, { useContext } from "react";
import ColorContext from './ColorContext';
const MyComponent = () => {
const colors = useContext(ColorContext);
return <div style={{ backgroundColor: colors.blue }}>Hello World</div>;
};
React's Context API allows you to share specific data from all levels of your application, thereby solving problems related to prop-drilling. The above example demonstrates how to create a context using the createContext
method and pass data as a prop【22†source】. The Provider
component enables the data in your context throughout your entire application. It wraps the context of your function and facilitates its functionality throughout【23†source】. The Consumer
component allows you to subscribe to a context’s changes, it will update and adjust your application based on the modification【24†source】. Lastly, the useContext
Hook is used in functional components to access the context within a functional component and works with a Provider
and Consumer
in one call【26†source】.
Higher Order Components
Higher-Order Component (HOC) is an advanced technique in React for reusing component logic. It's not a feature built into React, but rather a pattern that emerges from React’s compositional nature. A higher-order component is a function that takes a component and returns a new component.
More details on HOCs in plain JavaScript.
function withExtraPropAdded(component) {
const Component = component;
return function(props) {
return <Component {...props} extraProp="someValue" />;
}
}
const ComponentWithExtraProp = withExtraPropAdded(SomeComponent);
In this example, withExtraPropAdded
is a higher-order component that takes a component and returns a new component with an extra prop. The SomeComponent
is wrapped by the HOC and the resulting component ComponentWithExtraProp
has an additional prop extraProp
.
Conclusion
React.js offers a robust solution for building user interfaces in JavaScript. Its component-based architecture allows developers to build complex UIs from isolated and reusable pieces of code. Features like state, props, context, and hooks enable us to manage data and side-effects in our application. Libraries like React Router aid in creating single-page applications with multiple views and linking. By understanding these core concepts, you can leverage the full potential of React.js in your projects and create efficient, scalable, and maintainable web applications.
FAQ
What is React.js and what are its core concepts?
React.js is a popular JavaScript library for building user interfaces, especially for single-page applications. It's used for handling the view layer in web and mobile apps. React allows you to design simple views for each state in your application, and it will efficiently update and render the components when your data changes. The core aspects of React that every developer should grasp include components, state and props, hooks, routing with React-Router, Context API, and higher order components.
What are the popular methods for styling in React.js?
There are two popular methods for styling in React.js: CSS-in-JSX and Styled-Components. CSS-in-JS is a powerful tool in creating dynamic, visually appealing components by injecting your CSS right into your JSX. On the other hand, styled-components is a library for React and React Native that allows you to use component-level styles in your application that are written with a mixture of JavaScript and CSS.