SignIn.js I am redirecting the page using history.push but with it i am also passing the “username” but this username i am not able to see it in the redirected page “ADMIN.JS”.Since the username user enter that username i want to see it in the redirected page. All other content in admin.js are visible but only {username} is not visible.
JavaScript
x
165
165
1
import withRoot from './modules/withRoot';
2
// --- Post bootstrap -----
3
import React, { useState } from 'react';
4
import history from './history';
5
import { makeStyles } from '@material-ui/core/styles';
6
import Grid from '@material-ui/core/Grid';
7
import Link from '@material-ui/core/Link';
8
import { FormGroup, FormControl, ControlLabel } from "react-bootstrap";
9
import { Field } from 'react-final-form';
10
import Typography from './modules/components/Typography';
11
import AppFooter from './modules/views/AppFooter';
12
import AppAppBar from './modules/views/AppAppBar';
13
import Axios from 'axios';
14
import AppForm from './modules/views/AppForm';
15
import Button from '@material-ui/core/Button';
16
import { Alert } from 'react-bootstrap';
17
import { email, required } from './modules/form/validation';
18
import RFTextField from './modules/form/RFTextField';
19
import FormButton from './modules/form/FormButton';
20
import FormFeedback from './modules/form/FormFeedback';
21
import TextField from '@material-ui/core/TextField';
22
import Home from './Home';
23
import Dashb from './Dashb';
24
25
26
27
const useStyles = makeStyles((theme) => ({
28
form: {
29
marginTop: theme.spacing(6),
30
},
31
button: {
32
marginTop: theme.spacing(3),
33
marginBottom: theme.spacing(2),
34
},
35
feedback: {
36
marginTop: theme.spacing(2),
37
},
38
}));
39
40
const SignIn = (props) => {
41
const [username, setUsername] = useState("");
42
const [password, setPassword] = useState("");
43
const [status, setStatus] = useState(true);
44
const classes = useStyles();
45
let demo;
46
function validateForm() {
47
if(username.length==3 && password.length==6 )
48
return 1;
49
50
}
51
52
function setIncorrect() {
53
setStatus(false);
54
}
55
56
57
58
function setCorrect() {
59
setStatus(true);
60
}
61
62
63
64
65
66
const handleSubmit = (event) => {
67
event.preventDefault()
68
69
let user = Axios.get(
70
'http://localhost:9000/admin-service/admin/check/' +
71
username +
72
'/' +
73
password
74
)
75
.then(response => {
76
demo = response.data
77
if (demo == true) {
78
props.history.push({
79
pathname: '/admin',
80
username
81
});
82
console.log('####')
83
84
} else{
85
console.log('not true')
86
Functions();
87
}
88
})
89
.catch(error => {
90
console.log(error.response.data)
91
setIncorrect()
92
})
93
};
94
95
function Functions() {
96
alert("PLEASE ENTER CORRECT CREDENTIALS!!!!!!!!!!")
97
}
98
99
return (
100
<React.Fragment>
101
<AppAppBar />
102
<AppForm>
103
<React.Fragment>
104
<Typography variant="h3" gutterBottom marked="center" align="center">
105
Admin Sign In
106
</Typography>
107
</React.Fragment>
108
109
<form onSubmit={handleSubmit} className={classes.form} noValidate>
110
<TextField
111
variant="outlined"
112
margin="normal"
113
required
114
fullWidth
115
id="username"
116
label="Admin-Id"
117
name="username"
118
autoFocus
119
onChange={e => { setUsername(e.target.value); setCorrect() }}
120
/>
121
<TextField
122
variant="outlined"
123
margin="normal"
124
required
125
fullWidth
126
name="password"
127
label="Password"
128
type="password"
129
id="password"
130
autoComplete="current-password"
131
onChange={e => { setPassword(e.target.value); setCorrect() }}
132
/>
133
{(!status) && <Alert severity="error">Incorrect credentials. Please try again</Alert>}
134
135
<FormButton
136
type="submit"
137
className={classes.button}
138
disabled={!validateForm()}
139
size="large"
140
color="secondary"
141
fullWidth
142
>
143
Sign In
144
</FormButton>
145
</form>
146
147
148
149
<Typography align="center">
150
<Link underline="always" href="/premium-themes/onepirate/forgot-password/">
151
Forgot password?
152
</Link>
153
<p>NOTE-PASSWORD IS YOUR USER PIN</p>
154
</Typography>
155
</AppForm>
156
157
158
</React.Fragment>
159
160
161
);
162
}
163
164
export default SignIn;
165
Admin.js
JavaScript
1
22
22
1
import React from 'react';
2
import logo from './logo.svg';
3
import './App.css';
4
import Home from './Home.js';
5
import { Button } from 'react-bootstrap';
6
7
8
9
const Admin = props => {
10
const { username } =
11
(props.location && props.location.state) || {};
12
return (
13
<div>
14
<br/>
15
<br/>
16
<h2> Username </h2> {username}
17
<h1>child component-MILAN</h1>
18
</div>
19
);
20
}
21
export default Admin;
22
Advertisement
Answer
As you can read in getting-started:
Pass the state
:
JavaScript
1
7
1
history.push({
2
pathname: '/admin',
3
state: {
4
username: username
5
}
6
});
7
and read from the state
:
JavaScript
1
2
1
const username = history.location.state?.username
2