ReactJS¶
ReactJS is a popular JavaScript library for building user interfaces, particularly single-page applications. It allows developers to create reusable UI components. Below is a comprehensive list of the components and concepts associated with ReactJS:
Core Concepts¶
- JSX (JavaScript XML):
- Syntax extension that allows writing HTML elements in JavaScript.
- Example:
<h1>Hello, world!</h1>. - Components:
- The building blocks of a React application.
- Two types: Functional Components and Class Components.
Functional Components¶
- Defined as JavaScript functions.
- Return JSX to render the UI.
- Example:
Class Components¶
- ES6 classes that extend
React.Component. - Must contain a
render()method. - Example:
Component Lifecycle Methods (for Class Components)¶
- Mounting:
constructor()static getDerivedStateFromProps()render()componentDidMount()- Updating:
static getDerivedStateFromProps()shouldComponentUpdate()render()getSnapshotBeforeUpdate()componentDidUpdate()- Unmounting:
componentWillUnmount()
State and Props¶
- State: Local state within a component. Managed using
useStatehook (in functional components) or state property in class components.
Hooks (for Functional Components)¶
- useState: Manages state.
- useEffect: Handles side effects.
- useContext: Accesses context values.
- useReducer: Manages complex state logic.
- useCallback: Memoizes callback functions.
- useMemo: Memoizes expensive calculations.
- useRef: Accesses DOM elements directly.
- useLayoutEffect: Similar to
useEffectbut fires synchronously after all DOM mutations. - useImperativeHandle: Customizes the instance value that is exposed when using
ref. - useDebugValue: Displays a label for custom hooks in React DevTools.
Context API¶
- Provides a way to pass data through the component tree without passing props down manually at every level.
Higher-Order Components (HOC)¶
- A pattern that involves a function that takes a component and returns a new component.
function withLoading(Component) {
return function LoadingComponent(props) {
return <Component {...props} />;
}
}
Render Props¶
- A technique for sharing code between React components using a prop whose value is a function.
Fragments¶
- Allows grouping multiple elements without adding extra nodes to the DOM.
Portals¶
- Provides a way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
Error Boundaries¶
- Catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI.
class ErrorBoundary extends React.Component {
componentDidCatch(error, info) {
// Handle error
}
render() {
return this.props.children;
}
}
Refs¶
- Provides a way to access the DOM nodes or React elements created in the render method.
PropTypes¶
- Runtime type checking for props in React components.
Custom Hooks¶
- Functions that start with "use" and can call other hooks.
function useCustomHook() {
const [state, setState] = useState(initialState);
return [state, setState];
}
Concurrent Mode (Experimental)¶
- Allows React to work on multiple tasks simultaneously.
Suspense and Lazy¶
Suspense: Enables waiting for some code to load and declaratively specify a loading state.React.lazy(): Dynamically imports a component.
Understanding these components and concepts is crucial for effectively building and maintaining React applications.