gpt4 book ai didi

javascript - react.js this2.setState 不是函数

转载 作者:行者123 更新时间:2023-11-30 14:28:49 26 4
gpt4 key购买 nike

大家好,我试图在用户注销时将他重定向到主页,但是我遇到了一些问题。我在 React 路由器中使用重定向,我应该重定向到主页,因为重定向是正确的,但我却收到一个奇怪的错误,说 this2.setState 不是一个函数。

有人知道这是什么意思吗?感谢您的帮助!

import React from "react";
import {
Collapse,
Navbar,
NavbarToggler,
NavbarBrand,
Nav,
Button,
NavItem,
NavLink,
UncontrolledDropdown,
DropdownToggle,
DropdownMenu,
DropdownItem
} from "reactstrap";
import { withRouter } from "react-router";
import Icon from "react-icons-kit";
import { car } from "react-icons-kit/fa/car";
import { Redirect, Link } from "react-router-dom";
import app from "../config/dev";
export default class NavigatioBar extends React.Component {
constructor(props) {
super(props);
this.state = {
authenticated: false,
currentUser: null,
redirect: false,
isOpen: false
};
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState({
isOpen: !this.state.isOpen
});
}

signout() {
app
.auth()
.signOut()
.then(user => {
this.setState({ redirect: true });
});
}

componentWillMount() {
app.auth().onAuthStateChanged(user => {
if (user) {
this.setState({
currentUser: user,
authenticated: true
});
} else {
this.setState({
currentUser: null,
authenticated: false
});
}
});
}
render() {
const { redirect } = this.state;

if (redirect) {
return <Redirect to="/" />;
}
alert(this.state.authenticated);
const authenticated = this.state.authenticated;
return (
<div>
<Navbar color="primary" dark expand="md">
<div className="container">
<NavbarBrand href="/">
<Icon className="car-icon" icon={car} />
G66 CARS
</NavbarBrand>
<NavbarToggler onClick={this.toggle} />
<Collapse isOpen={this.state.isOpen} navbar>
<Nav className="ml-auto" navbar>
<NavItem>
<NavLink href="/cars">CARS</NavLink>
</NavItem>
<NavItem>
<NavLink href="/">FINANCE</NavLink>
</NavItem>
<NavItem>
<NavLink href="/">SERVICE</NavLink>
</NavItem>
<NavItem>
<NavLink href="/">PARTS</NavLink>
</NavItem>
<UncontrolledDropdown nav>
<DropdownToggle nav caret>
ABOUT
</DropdownToggle>

<DropdownMenu>
{authenticated ? (
<DropdownItem onClick={this.signout}>
<Button classname="btn-btn-primary">SignOut </Button>
</DropdownItem>
) : (
<DropdownItem>
<Link to="/login"> Login </Link>
</DropdownItem>
)}

<DropdownItem>ABOUT US</DropdownItem>
<DropdownItem>STAFF</DropdownItem>
<DropdownItem>CONTACT US</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
</Nav>
</Collapse>
</div>
</Navbar>
</div>
);
}
}

最佳答案

signout = () => {
app.auth().signOut().then((user) => {
this.setState ({ redirect: true })
})
}

注销没有这个上下文。

关于javascript - react.js this2.setState 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com