React Assignment
React Assignment
React Assignment
return (
<Router>
<div>
<Route exact path="/" component={Home} />
<Route
path="/dashboard"
render={() =>
loggedIn ? <Dashboard logout={logout} /> : <Redirect to="/login" />
}
/>
<Route
path="/login"
render={() => (loggedIn ? <Redirect to="/dashboard" /> : <Login login={login} />)}
/>
</div>
</Router>
);
};
Home.js:
import React from 'react';
Dashboard.js:
import React from 'react';
Login.js:
import React from 'react';
2. Setting up project:
npx create-react-app my-app
cd my-app
npm install react-router-dom
Setting up routing:
import React, { useState } from 'react';
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
import LoginForm from './LoginForm';
import SignupForm from './SignupForm';
import Dashboard from './Dashboard';
function App() {
const [loggedIn, setLoggedIn] = useState(false);
const [users, setUsers] = useState([]);
return (
<Router>
<div>
<Switch>
<Route exact path="/">
<Redirect to="/login" />
</Route>
<Route path="/login">
<LoginForm onLogin={handleLogin} />
</Route>
<Route path="/signup">
<SignupForm onSignup={handleSignup} />
</Route>
<Route path="/dashboard">
{loggedIn ? <Dashboard users={users} /> : <Redirect to="/login" />}
</Route>
</Switch>
</div>
</Router>
);
}
Creating a LoginForm.js:
import React, { useState } from 'react';
return (
<div>
<h2>Login</h2>
<form onSubmit={handleSubmit}>
<input type="text" placeholder="Username" value={username} onChange={(e) =>
setUsername(e.target.value)} />
<input type="password" placeholder="Password" value={password} onChange={(e) =>
setPassword(e.target.value)} />
<button type="submit">Login</button>
</form>
</div>
);
};
return (
<div>
<h2>Sign Up</h2>
<form onSubmit={handleSubmit}>
<input type="text" placeholder="Username" value={username} onChange={(e) =>
setUsername(e.target.value)} />
<input type="password" placeholder="Password" value={password} onChange={(e) =>
setPassword(e.target.value)} />
<button type="submit">Sign Up</button>
</form>
</div>
);
};
Dashboard.js component:
import React from 'react';
const Dashboard = ({ users }) => {
return (
<div>
<h2>Dashboard</h2>
<ul>
{users.map((user, index) => (
<li key={index}>{user.username}</li>
))}
</ul>
</div>
);
};
componentDidMount() {
console.log('Component Did Mount');
this.setState({ message: 'Component is mounted!' });
}
componentDidUpdate() {
console.log('Component Did Update');
}
componentWillUnmount() {
console.log('Component Will Unmount');
}
render() {
console.log('Render');
return <div>{this.state.message}</div>;
}
}
4. Parent Component:
import React from 'react';
import ChildComponent from './ChildComponent';
const ParentComponent = () => {
const product = {
id: 1,
name: 'Product Name',
price: 10.99
};
return (
<div>
<h2>Parent Component</h2>
<ChildComponent product={product} />
</div>
);
};
Child component:
import React from 'react';
return (
<div>
<h3>Child Component</h3>
<p>ID: {product.id}</p>
<p>Name: {product.name}</p>
<p>Price: ${product.price}</p>
</div>
);
};
5. Using Axios:
import React, { useState } from 'react';
import axios from 'axios';
return (
<div>
<h2>Post Request using Axios</h2>
<form onSubmit={handleSubmit}>
<input type="text" name="title" placeholder="Title" value={postData.title}
onChange={handleChange} />
<textarea name="body" placeholder="Body" value={postData.body}
onChange={handleChange} />
<button type="submit">Submit</button>
</form>
</div>
);
};