【发布时间】:2020-06-13 10:45:53
【问题描述】:
我希望我的孩子在我的父母更新其状态时重新渲染。看起来很简单,但我还没有让它工作。
在 App.js 中,我在 componentWillMount 中进行 API 调用。然后我希望“PrivateRoute”更新状态“auth”,但似乎 componentDidUpdate 从未运行,我总是被重定向。
代码的目的是 API 调用检查用户是否拥有有效的身份验证令牌,然后将“isLoggedIn”状态设置为 true 或 false。这应该使子“PrivateRoute”重定向(如果 isLoggedIn 为假)或呈现另一个页面(如果 isLoggedIn 为真)。
我检查了很多其他类似的问题,但到目前为止还没有解决方案。
我的 app.js:
import React, { Component } from "react";
import {
BrowserRouter as Router,
Route,
Switch,
Link,
Redirect,
} from "react-router-dom";
import axios from "axios";
import PrivateRoute from "./components/PrivateRoute";
// Pages
import IndexPage from "./pages/index";
import HomePage from "./pages/home";
class App extends Component {
constructor() {
super();
console.log("Constructor");
this.state = {
loggedInStatus: false,
test: "TEST",
};
}
// Checks if user is logged in
checkAuth() {
let token = localStorage.getItem("token");
let isLoggedIn = false;
if (token === null) token = "bad";
console.log("Making API call");
// API call
axios
.get("http://localhost:8000/authentication/checkAuth", {
headers: { Authorization: "token " + token },
})
.then((res) => {
console.log("Updateing state");
this.setState({ loggedInStatus: true });
})
.catch((error) => {
console.log("Updating state");
this.setState({ loggedInStatus: false });
});
return isLoggedIn;
}
componentWillMount() {
this.checkAuth();
}
render() {
//console.log("Render");
// console.log("isLoggedIn: ", this.state.loggedInStatus);
return (
<Router>
<Switch>
<PrivateRoute
exact
path="/home"
component={HomePage}
auth={this.state.loggedInStatus}
/>
<Route exact path="/" component={IndexPage} />
</Switch>
</Router>
);
}
}
export default App;
PrivateRoute.jsx:
import { Redirect } from "react-router-dom";
import React, { Component } from "react";
class PrivateRoute extends Component {
state = {};
constructor(props) {
super(props);
this.state.auth = false;
}
// Update child if parent state is updated
componentDidUpdate(prevProps) {
console.log("Component did update");
if (this.props.auth !== prevProps.auth) {
console.log("Child component update");
this.setState({ auth: this.props.auth ? true : false });
}
}
render() {
console.log("Props: ", this.props);
console.log("State: ", this.state);
//alert("this.props.auth: ", this.props.auth);
//alert("TEST: ", this.props.test);
if (this.props.auth) {
return <h1>Success!</h1>;
//return <Component {...this.props} />;
} else {
return (
<Redirect
to={{ pathname: "/", state: { from: this.props.location } }}
/>
);
}
}
}
export default PrivateRoute;
【问题讨论】:
-
使用
componentDidMount而不是componentWillMount -
另外,您的 PrivateRoute 中不需要“this.state.auth”,因为您可以使用道具 (this.props.auth)
-
@RedBaron componentDidMount 不起作用,因为在第一次渲染时它会重定向。
标签: javascript reactjs state