React Basics with Examples:
Please note that this list assumes you’re using React version 17 or newer and have a basic understanding of JavaScript and React fundamentals.
- Importing React:
import React from 'react';
import ReactDOM from 'react-dom';
- Functional Component:
const MyComponent = () => {
return (
Hello, React!
);
};
- Class Component:
class MyComponent extends React.Component {
render() {
return (
Hello, React!
);
}
}
- Rendering a Component:
ReactDOM.render( , document.getElementById('root'));
- Props:
// Passing props
// Accessing props in functional component
const MyComponent = (props) => {
return {props.title}
;
};
// Accessing props in class component
class MyComponent extends React.Component {
render() {
return {this.props.title}
;
}
}
- State:
// In class component
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
counter: 0,
};
}
// In functional component (useState Hook)
import React, { useState } from 'react';
const MyComponent = () => {
const [counter, setCounter] = useState(0);
};
- Event Handling:
// In functional component
const MyComponent = () => {
const handleClick = () => {
console.log('Button clicked');
};
return ;
};
// In class component
class MyComponent extends React.Component {
handleClick() {
console.log('Button clicked');
}
render() {
return ;
}
}
- useEffect Hook:
import React, { useEffect } from 'react';
const MyComponent = () => {
useEffect(() => {
// Perform side effect here, e.g., fetch data, update DOM, etc.
}, []); // Pass an empty array to run only once (on mount and unmount)
return ...;
};
- useRef Hook:
import React, { useRef } from 'react';
const MyComponent = () => {
const inputRef = useRef();
const handleButtonClick = () => {
inputRef.current.focus();
};
return (
);
};
- Conditional Rendering:
const MyComponent = (props) => {
return {props.condition ? 'Truthy' : 'Falsy'};
};
- Looping through Lists:
const MyComponent = (props) => {
return (
{props.items.map((item, index) => (
- {item}
))}
);
};
- Context API:
// Creating a context
const MyContext = React.createContext();
// Providing context
// Consuming context in functional component
import { useContext } from 'react';
const MyComponent = () => {
const value = useContext(MyContext);
// Use the value here
};
// Consuming context in class component
class MyComponent extends React.Component {
render() {
return (
{(value) => {
// Use the value here
return {value};
}}
);
}
}
In this part, we’re covering how to create a context using React.createContext()
, provide context value using a context provider, and consume context values in both functional and class components. The useContext Hook is used in functional components, while the Consumer component is used in class components.
- Custom Hooks:
// Create custom hook
function useCustomHook() {
// Hook logic goes here
}
// Use custom hook
const MyComponent = () => {
const result = useCustomHook();
};
- useMemo Hook:
import React, { useMemo } from 'react';
const MyComponent = (props) => {
const computedValue = useMemo(() => {
// Expensive computation goes here
}, [props.dependency]);
return {computedValue};
};
- useCallback Hook:
import React, { useCallback } from 'react';
const MyComponent = (props) => {
const memoizedCallback = useCallback(() => {
// Function implementation
}, [props.dependency]);
return ...;
};
- React.memo:
// Higher-Order Component for functional components
const MyComponent = React.memo((props) => {
// Component implementation
});
// Prevent re-rendering if props are the same
- Error Boundary:
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
return { hasError: true };
}
componentDidCatch(error, info) {
// Log the error
}
render() {
if (this.state.hasError) {
return Something went wrong.
;
}
return this.props.children;
}
}
// Usage
- Portals:
import ReactDOM from 'react-dom';
const MyPortal = (props) => {
return ReactDOM.createPortal(
props.children,
document.getElementById('portal-root')
);
};
// Usage
Content inside a portal