Wdf Exercises
Wdf Exercises
function useFetch(url) {
useEffect(() => {
fetch(url)
.then(res => {
return res.json();
})
}, [url]);
return (
<div>
<h1>Todo Titles</h1>
<ul>
{todos.map(todo => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
</div>
);
AXIOS
FOR GET
useEffect(() => {
axios.get('https://jsonplaceholder.typicode.com/todos')
.then(response => {
setTodos(response.data);
setLoading(false);
})
.catch(err => {
setError(err);
setLoading(false);
});
}, []);
return (
<div>
<h1>Todo Titles</h1>
<ul>
{todos.map(todo => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
</div>
);
FOR PUT
function App() {
axios.put('https://jsonplaceholder.typicode.com/todos/1', {
id: 1,
completed: true,
userId: 1
})
.then(response => {
})
.catch(error => {
console.error('Error:', error);
});
};
return (
<div>
</div>
);
return (
<div>
<h1>Counter: {count}</h1>
<button onClick={onIncrement}>Increase</button>
</div>
);
};
return (
);
App.js
function App() {
return (
<div>
<CounterContainer />
</div>
);
}
export default App;
return (
<div>
<h2>{user.name}</h2>
<p>{user.email}</p>
</div>
);
// 🧪 Mock data
const mockUser = {
email: 'john@example.com'
};
test('renders user name and email', () => {
expect(screen.getByText('John Doe')).toBeInTheDocument();
expect(screen.getByText('john@example.com')).toBeInTheDocument();
});
REACT-NATIVE
Return
};
UNIT 4
EVENT BUBBLE UP
function App() {
alert('Parent Clicked!');
};
alert('Child Clicked!');
};
return (
</div>
);
HOC
withGreeting.js – HOC
function withGreeting(WrappedComponent) {
return (
<div>
</div>
);
};
function MyComponent() {
function App() {
return (
<div>
<EnhancedComponent />
</div>
);
MyComponent.module.css
.title {
color: blue;
font-size: 24px;
text-align: center;
.button {
background-color: lightgreen;
border: none;
cursor: pointer;
MyComponent.js
function MyComponent() {
return (
<div>
</div>
);
App.js
function App() {
return (
<div>
<MyComponent />
</div>
);
});
function SimpleForm() {
return (
<Formik
validationSchema={validationSchema}
onSubmit={(values) => {
}}
>
<Form>
<div>
<label>Name:</label>
</div>
<div>
<label>Email:</label>
</div>
<button type="submit">Submit</button>
</Form>
</Formik>
);
EVENTLISTENER
CLICK EVENT
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
button.addEventListener('click', () => {
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Mouse Events</title>
<style>
#box {
width: 200px;
height: 200px;
background-color: lightblue;
text-align: center;
line-height: 200px;
font-size: 20px;
</style>
</head>
<body>
<script>
box.addEventListener('mouseover', () => {
box.style.backgroundColor = 'lightgreen';
});
// Mouseout event
box.addEventListener('mouseout', () => {
box.style.backgroundColor = 'lightblue';
});
</script>
</body>
</html>
function OneWayBindingExample() {
return (
<div>
</button>
</div>
);
function TwoWayBindingExample() {
return (
<div>
<input
type="text"
/>
<p>Hello, {name}</p>
</div>
);
function Welcome() {
return (
<div>
<h1>Welcome to React!</h1>
</div>
);
App.js
function App() {
return (
<div>
<Welcome />
</div>
);
render() {
return (
<div>
<h1>Welcome to React!</h1>
</div>
);
App.js
function App() {
return (
<div>
<Welcome />
</div>
);
SETSATE EXAMPLE
constructor() {
super();
this.state = {
count: 0
};
increment = () => {
};
render() {
return (
<div>
<h2>Count: {this.state.count}</h2>
<button onClick={this.increment}>Increment</button>
</div>
);
function Counter() {
setCount(count + 1);
};
setCount(count - 1);
};
setCount(0);
};
return (
<div>
<h2>Count: {count}</h2>
</div>
);
USEEFFECT EXAMPLE
function HelloEffect() {
useEffect(() => {
console.log('Component mounted!');
alert('Welcome! 👋');
return () => {
};
return (
<div>
<h2>Check your console and alert 🔍</h2>
</div>
);
function CounterWithEffect() {
useEffect(() => {
}, [count]);
return (
<div>
<h2>Count: {count}</h2>
</div>
);
}
export default CounterWithEffect;
PROPS EXAMPLE
APP.JS
function App() {
return (
<div>
</div>
);
GREETINGS.JS
function Greeting(props) {
GREETINGS.JS
return (
<div>
<h2>Hello, {name}!</h2>
</div>
);
// ✅ Define PropTypes
Greeting.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number
};
APP.JS
import React from 'react';
function App() {
return (
<div>
</div>
);