I am taking email and password from the user and if password matches user will be redirected to account page.
Login page:
import React, { useState, useEffect } from "react";
import Axios from "axios";
import { useNavigate } from 'react-router-dom';
import Account from "./account";
const Login = () => {
const navigate = useNavigate();
const [products, setProducts] = useState([]);
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const Submit = async (e) => {
e.preventDefault();
const { data } = await Axios.get(
`http://localhost:8080/api/login/${email}`
);
let parseData = JSON.parse(data.response);
setProducts(parseData);
console.log(password);
console.log(products.password);
if(products.password==password){
window.isLoggedin=1;
<Account props={products} />
navigate(`/account`)
}
else{
navigate(`/sign-in`)
}
};
return (
<form >
<h3>Sign In</h3>
<div className="mb-3">
<label>Email</label>
<input
type="email"
name="email"
className="form-control"
placeholder="Enter email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>
<div className="mb-3">
<label>Password</label>
<input
type="password"
name="password"
className="form-control"
placeholder="Enter password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<div className="d-grid">
<button type="submit" onClick={Submit} className="btn btn-primary">
Sign In
</button>
</div>
<p className="forgot-password text-right">
Not registered <a href="/sign-up">sign up?</a>
</p>
</form>
)
}
export default Login;
Account Page:
import React, { useState, useEffect } from "react";
import './account.css';
import { useNavigate } from 'react-router-dom';
import Login from "./login";
const Account = (props) => {
const navigate = useNavigate();
const check =()=>{
console.log(props.email);
if(window.isLoggedin==0)
navigate(`/sign-in`)
}
useEffect(() => {
check();
}, []);
return (
<div class="container d-flex justify-content-center">
<div class="card p-3 py-4">
<div class="text-center">
<h3 class="mt-2">Username</h3>
<div class="row mt-3 mb-3">
<div class="col-md-4">
<h5>Email</h5>
<span class="num">10</span>
</div>
<div class="col-md-4">
<h5>Location</h5>
<span class="num">10</span>
</div>
<div class="col-md-4">
<h5>{props.products}</h5>
<span class="num">10</span>
</div>
</div>
<hr class="line"></ hr>
<div class="social-buttons mt-5">
<button class="neo-button"><i class="fa fa-facebook fa-1x"></i> </button>
<button class="neo-button"><i class="fa fa-linkedin fa-1x"></i></button>
<button class="neo-button"><i class="fa fa-google fa-1x"></i> </button>
<button class="neo-button"><i class="fa fa-youtube fa-1x"></i> </button>
<button class="neo-button"><i class="fa fa-twitter fa-1x"></i> </button>
</div>
<div class="profile mt-5">
<button class="profile_button px-5" >Log Out</button>
</div>
</div>
</div>
</div>
);
}
export default Account;
Here i want to pass products array from Login page to Account page.
Please guide me how to send data from one page to another in react JS.
I did some solutions availabe but i am unable to pass the data.
4
Answers
You can use localStorage to save your products array from Login component then getlocalStorage at Acount component.(context,useReducer,redux).
You aren’t returning the
Account
component here:Return it like this:
This Stackoverflow post describes how to pass data from one page to another, using
useNavigate
anduseLocation
.It is a bad idea to return user passwords from the server. So try to refactor your code so that the app does not spread your user secret to the whole world. Password validation must be done on the server-side and return the validation status as part of the response. The request method should use the
post
method and post the user credentials as the request payload.And to pass data to
Account
component you can usenavigate("/account", { state: parseData })
Login.jsx
Another approach is to use react-tracked, a state usage tracking library. And if you want a persisting state, you can use useReducer with localStorage
./store.js
./useAuth.js
Login.js
Account.js