REACT
REACT
React is a JavaScript library developed by Facebook for building user interfaces, specifically
single-page applications (SPAs) where you need a fast, interactive experience. It allows
developers to build web applications by creating reusable UI components that update and
render efficiently in response to data changes.
3. Declarative UI: React makes it easier to design UIs by using a declarative approach.
You describe how the UI should look for a given state, and React takes care of updating
the DOM when that state changes, reducing the need for manual DOM manipulation.
4. JSX Syntax: React uses JSX, a syntax that allows you to write HTML elements within
JavaScript. This makes it easier to work with and understand, especially for developers
familiar with HTML and JavaScript.
5. Cross-platform Development: With React Native, you can use your React
knowledge to build mobile apps for iOS and Android, making React a versatile choice
for both web and mobile development.
A Hook is a feature in React that allows you to "hook into" React state and lifecycle features
from functional components. Before hooks were introduced in React 16.8, you could only use
state and lifecycle methods in class components. Hooks allow functional components to have
the same capabilities, making them more powerful and flexible.
Here’s a breakdown of the most commonly used hooks:
1. useState:
o This hook allows you to add state to functional components.
o It returns an array where the first element is the current state value, and the
second element is a function to update that state.
Javascript-
const [count, setCount] = useState(0);
2. useEffect:
o This hook lets you perform side effects in functional components (like fetching
data, subscribing to events, or manually changing the DOM).
o It's similar to lifecycle methods like componentDidMount, componentDidUpdate,
and componentWillUnmount in class components.
Javascript-
3. useContext:
o This hook allows you to subscribe to React context, making it easier to share
global state across the app without having to pass props manually through each
level of the component tree.
javascript
const theme = useContext(ThemeContext);
or
import React, { createContext, useContext } from 'react';
// Create context
const MyContext = createContext();
function App() {
return (
// Provide value
<MyContext.Provider value="Hello, World!">
<Child />
</MyContext.Provider>
);
}
function Child() {
// Consume value with useContext
const value = useContext(MyContext);
return <div>{value}</div>;
}
javascript
const [state, dispatch] = useReducer(reducer, initialState);
5. useRef:
o This hook lets you persist values between renders without causing a re-render
when the value changes. It’s often used for accessing DOM elements directly or
keeping a reference to a value that doesn’t trigger a re-render.
javascript
let ref = useRef(0);
function handleClick() {
ref.current = ref.current + 1;
alert('You clicked ' + ref.current + ' times!');
}
return (
<button onClick={handleClick}>
Click me!
</button>