I have these two forms and I wanted to redirect the user to the homepage after the first form submit handleSubmit
. I have already declared withRouter
and used it. But, it still has the error:
TypeError: history.push is not a function
Below are the codes:
JavaScript
x
64
64
1
import { withRouter } from "react-router-dom";
2
3
4
import firebase from "firebase/app";
5
6
const Session = ({ id }) => {
7
const classes = useStyles();
8
9
other codes above .
10
11
const handleSubmit = (e) => {
12
e.preventDefault();
13
try {
14
const userRef = firestore.collection("users").doc(id);
15
const ref = userRef.set(
16
{
17
items: {
18
id,
19
variable names here
20
},
21
},
22
23
{ merge: true }
24
);
25
26
console.log(" saved");
27
stocksCounter();
28
history.push("/"); <-- history.push
29
} catch (err) {
30
console.log(err);
31
}
32
};
33
34
35
36
//2nd submit-----------------------------------------------------------
37
other codes here
38
const handleSubmit2 = (e) => {
39
e.preventDefault();
40
try {
41
const userRef = firestore.collection("users").doc(scanResult);
42
const ref = userRef.set(
43
{
44
items: {
45
..variable names here
46
},
47
},
48
{ merge: true }
49
);
50
51
console.log(" saved");
52
stocksCounter();
53
} catch (err) {
54
console.log(err);
55
}
56
};
57
58
return (
59
codes here .
60
);
61
};
62
63
export default withRouter(Session);
64
Advertisement
Answer
use the useHistory
hook.
JavaScript
1
3
1
const history = useHistory();
2
history.push('/')
3