I am getting TypeError: Cannot read property 'push' of undefined. what should I do?
I am getting TypeError: Cannot read property 'push' of undefined. what should I do?
import "./Navbar.css";
import * as ReactBootstrap from "react-bootstrap";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import {
FaFacebookF,
FaTwitter,
FaPinterest,
FaInstagram,
FaCcPaypal,
FaCcMastercard,
FaCcVisa,
FaCreditCard,
FaStar,
FaCircle,
FaRegHeart,
} from "react-icons/fa";
import {
BiSearchAlt,
BiUserCircle,
BiHeart,
BiShoppingBag,
BiEnvelope,
BiLoader,
} from "react-icons/bi";
import React from "react";
import ReactDOM from "react-dom";
import { withRouter } from "react-router-dom";
import SingleProduct from "./SingleProduct";
import {
BrowserRouter as Router,
Route,
Switch,
Redirect,
} from "react-router-dom";
import history from "./History";
const Navbar = (props) => {
let callsingleproduct = () => {
console.log("SINGLE PRODUCT :|: ", props);
props.history.push("/SingleProduct");
};
return (
<div className='App'>
{" "}
<Container fluid className='navbar1 nav1'>
{" "}
<ReactBootstrap.Navbar
collapseOnSelect
expand='lg'
className=' justify-content-center '>
{" "}
<ReactBootstrap.Navbar className='nav1 pt-2 nav'>
<BiUserCircle className='mr-2 mt-1 login ' />
LOGIN
</ReactBootstrap.Navbar>{" "}
<ReactBootstrap.Navbar.Toggle aria-controls='responsive-navbar-nav ' />{" "}
<ReactBootstrap.Navbar.Collapse id='responsive-navbar-nav'>
{" "}
<ReactBootstrap.Nav className='mx-auto pl-5'>
{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
HOME
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav '>
SHOP
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
PAGES
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-4'>
<img src='images/capture.PNG' width='80%' alt='pic' />
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
BLOG
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
LOOKBOOK
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
CONTACT
</ReactBootstrap.Nav.Link>{" "}
</ReactBootstrap.Nav>{" "}
<ReactBootstrap.Nav>
{" "}
<ReactBootstrap.Nav.Link>
{" "}
<Row className='icon'>
{" "}
<ReactBootstrap.Nav.Link className='mx-1'>
<BiUserCircle className=' login' />
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-1'>
<BiSearchAlt className=' login' />
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-1'>
<BiHeart className=' login' />
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-1'>
<BiShoppingBag
className='login'
onClick={() => {
callsingleproduct();
}}
/>{" "}
</ReactBootstrap.Nav.Link>{" "}
</Row>{" "}
</ReactBootstrap.Nav.Link>{" "}
</ReactBootstrap.Nav>{" "}
</ReactBootstrap.Navbar.Collapse>{" "}
</ReactBootstrap.Navbar>{" "}
</Container>{" "}
</div>
);
};
export default Navbar;
Error
此错误意味着 - history
属性未定义(未传入属性)。
确保您正确传递了它。此外,您可以为 props 设置默认值以避免错误和破损。
如果您使用的是 react-router-dom
,则将导出语句更改为:
export default withRouter(Navbar);
withRouter 将 history
属性发送到子元素。
import "./Navbar.css";
import * as ReactBootstrap from "react-bootstrap";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import {
FaFacebookF,
FaTwitter,
FaPinterest,
FaInstagram,
FaCcPaypal,
FaCcMastercard,
FaCcVisa,
FaCreditCard,
FaStar,
FaCircle,
FaRegHeart,
} from "react-icons/fa";
import {
BiSearchAlt,
BiUserCircle,
BiHeart,
BiShoppingBag,
BiEnvelope,
BiLoader,
} from "react-icons/bi";
import React from "react";
import ReactDOM from "react-dom";
import { withRouter } from "react-router-dom";
import SingleProduct from "./SingleProduct";
import {
BrowserRouter as Router,
Route,
Switch,
Redirect,
} from "react-router-dom";
import history from "./History";
const Navbar = (props) => {
let callsingleproduct = () => {
console.log("SINGLE PRODUCT :|: ", props);
props.history.push("/SingleProduct");
};
return (
<div className='App'>
{" "}
<Container fluid className='navbar1 nav1'>
{" "}
<ReactBootstrap.Navbar
collapseOnSelect
expand='lg'
className=' justify-content-center '>
{" "}
<ReactBootstrap.Navbar className='nav1 pt-2 nav'>
<BiUserCircle className='mr-2 mt-1 login ' />
LOGIN
</ReactBootstrap.Navbar>{" "}
<ReactBootstrap.Navbar.Toggle aria-controls='responsive-navbar-nav ' />{" "}
<ReactBootstrap.Navbar.Collapse id='responsive-navbar-nav'>
{" "}
<ReactBootstrap.Nav className='mx-auto pl-5'>
{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
HOME
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav '>
SHOP
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
PAGES
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-4'>
<img src='images/capture.PNG' width='80%' alt='pic' />
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
BLOG
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
LOOKBOOK
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-3 pt-4 nav'>
CONTACT
</ReactBootstrap.Nav.Link>{" "}
</ReactBootstrap.Nav>{" "}
<ReactBootstrap.Nav>
{" "}
<ReactBootstrap.Nav.Link>
{" "}
<Row className='icon'>
{" "}
<ReactBootstrap.Nav.Link className='mx-1'>
<BiUserCircle className=' login' />
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-1'>
<BiSearchAlt className=' login' />
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-1'>
<BiHeart className=' login' />
</ReactBootstrap.Nav.Link>{" "}
<ReactBootstrap.Nav.Link className='mx-1'>
<BiShoppingBag
className='login'
onClick={() => {
callsingleproduct();
}}
/>{" "}
</ReactBootstrap.Nav.Link>{" "}
</Row>{" "}
</ReactBootstrap.Nav.Link>{" "}
</ReactBootstrap.Nav>{" "}
</ReactBootstrap.Navbar.Collapse>{" "}
</ReactBootstrap.Navbar>{" "}
</Container>{" "}
</div>
);
};
export default Navbar;
Error
此错误意味着 - history
属性未定义(未传入属性)。
确保您正确传递了它。此外,您可以为 props 设置默认值以避免错误和破损。
如果您使用的是 react-router-dom
,则将导出语句更改为:
export default withRouter(Navbar);
withRouter 将 history
属性发送到子元素。