? My Complete React Notes – Free Resource for Developers
? My Complete React Notes – Free Resource for Developers
Then enter the information that is required after that your first react app
will be ready. Then you have to run following command after opening the
created app in vs code.
npm install
2. Components in React:
Basic Example:
function App() {
return (
<div>
<Greeting />
</div>
);
}
3. Props in React:
Props (short for "properties") allow components to receive data from their
parent components. Props are read-only and help make components
reusable.
function App() {
return (
<div>
<Greeting name="Zohaib" />
<Greeting name="Ali" />
</div>
);
}
function App() {
return (
<div>
<Greeting name="Zohaib" />
<Greeting name="Ali" />
</div>
);
}
React Hooks are functions that allow you to use state and other React
features in functional components. Introduced in React 16.8, Hooks
eliminate the need for class components while maintaining the same
functionality.
useState:
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count +
1)}>Increment</button>
<button onClick={() => setCount(count –
1)}>Decrement</button>
</div>
);
}
Explanation:
useEffect Hook:
The useEffect Hook is one of the most powerful and commonly used hooks
in React. It allows you to perform side effects in functional components,
such as:
• Fetching data
• Subscribing to events
• Updating the DOM
• Setting up timers
useEffect(() => {
// Side effect logic here
}, [dependencies]);
• The first argument is a function where you write the side effect
code.
• The second argument (optional) is a dependency array that
determines when the effect should run.
useEffect(() => {
console.log("Effect ran!");
});
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count +
1)}>Increment</button>
</div>
);
};
useEffect(() => {
document.title = `Count: ${count}`;
}, [count]); // Runs when `count` changes
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count +
1)}>Increment</button>
</div>
);
};
useRef Hook:
Syntax:
useEffect(() => {
inputRef.current.focus(); // Automatically focus the
input field on mount
}, []);
return (
<div>
<input ref={inputRef} type="text" placeholder="Type
here..." />
<button onClick={() => inputRef.current.focus()}>Focus
Input</button>
</div>
);
};
Example:
function ThemedButton() {
const theme = useContext(ThemeContext);
return <button style={{ background: theme === 'dark' ?
'black' : 'white', color: theme === 'dark' ? 'white' :
'black' }}>Click Me</button>;
}
function App() {
return (
<ThemeContext.Provider value="dark">
<ThemedButton />
</ThemeContext.Provider>
);
}
Example:
import { useState } from 'react'
import './App.css'
function App() {
const [first, setfirst] = useState(true)
return (
<>
{first?<button>Show btn is
true</button>:<button>Showbtn is false</button>}
<div className="card">
<button onClick={() => setfirst(!first)}>
Toggle Showbtn
</button>
</div>
</>
)
}
function App() {
const [todos, settodo] = useState([
{
title:"I am todo",
desc:"I am desc"
},
{
title:"I am todo 1",
desc:"I am desc 1"
},
{
title:"I am todo 2",
desc:"I am desc 2"
},
])
const Todo=({todo})=>{
return(
<>
<div className='todo'>{todo.title}</div>
<div className='todo'>{todo.desc}</div>
</>
)
}
return (
<>
<div className="card">
{todos.map(todo=>{
return <Todo key={todo.title} todo={todo}/>
}
)}
</div>
</>
)
}
function App() {
const [todos, settodo] = useState([
{
title:"I am todo",
desc:"I am desc"
},
{
title:"I am todo 1",
desc:"I am desc 1"
},
{
title:"I am todo 2",
desc:"I am desc 2"
},
])
return (
<>
<div className="card">
{todos.map(todo=>{
return(
<>
<div key={todo.title}>
<div className='todo'>{todo.title}</div>
<div className='todo'>{todo.desc}</div>
</div>
</>
)
}
)}
</div>
</>
)
}
function App() {
const handleClick=()=>{
alert("I am clicked.")
}
const handleMouse=()=>{
alert("Mouse is over me.")
}
const [name, setname] = useState("Zohaib");
const handleInput=(e)=>{
setname(e.target.value)
}
return (
<>
<div className="container">
<div> <button onClick={handleClick}>Click
me</button></div>
<div><button onMouseOver={handleMouse}>Mouse
over</button></div>
<div><input type="text" value={name} /></div>
</div>
</>
)
}
This React code defines an App component with three interactive elements:
The useState hook is used to manage the name state, which is initially set to
"Zohaib". However, the input field doesn't currently allow the user to
change the state because the handleInput function isn't connected to the
onChange event of the input.
You can install this library by running the following command in the
terminal.
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById("root")
);
Creating Routes:
In your App.js, set up the routes using Routes and Route components:
Home.js
About.js
Contact.js
Adding Navigation:
return (
<div>
<h1>Home Page</h1>
<button onClick={() => navigate("/about")}>Go to
About</button>
</div>
);
};
export default Home;
The useContext hook in React provides a way to share state and functions
across components without having to pass props manually at every level. It
is part of the React Context API and is useful for managing global state,
such as theme settings, user authentication, or language preferences.
i. Create a Context
ii. Provide the Context value
iii. Consume the Context using useContext
So we can easily use the context api. So, our app.jsx file will be
function App() {
const [count, setCount] = useState(0);
return (
<>
<div className="card">
<button onClick={() => setCount((count) => count +
1)}>
count is {count}
</button>
</div>
</counterContext.Provider>
</>
)
}
Syntax:
const memoizedValue = useMemo(() => computeFunction(),
[dependencies]);
return (
<div style={{ background: theme ? "#333" : "#fff",
color: theme ? "#fff" : "#000", padding: "20px" }}>
<h2>Factorial of {number}: {factorial(number)}</h2>
<button onClick={() => setNumber(number + 1)}>Increase
Number</button>
<button onClick={() => setTheme(!theme)}>Toggle
Theme</button>
</div>
);
};
Problems:
return (
<div style={{ background: theme ? "#333" : "#fff",
color: theme ? "#fff" : "#000", padding: "20px" }}>
<h2>Factorial of {number}: {factorial}</h2>
<button onClick={() => setNumber(number + 1)}>Increase
Number</button>
<button onClick={() => setTheme(!theme)}>Toggle
Theme</button>
</div>
);
};
🔰 React Basics
11. What is React reconciliation and how does React handle re-renders?
12.What are controlled and uncontrolled components? Give examples.
13.What is memoization in React? Explain React.memo and useMemo.
14.What is React Router? How does client-side routing work in React?
15. What is the difference between lifting state up and using context?
🚀 Performance Optimization