【发布时间】:2019-01-28 23:39:58
【问题描述】:
我有名为 isAuthenticated 的道具,它还表明某些情况下的警告仍然存在于我的控制台中。请检查一下。
import React,{Component} from "react";
import {BrowserRouter as Router, Switch, Route} from "react-router-dom";
import LogoutHome from './Logout_Home';
import SignIn from '../Components/SignIn';
import jwt_decode from 'jwt-decode';
import {setCurrentUser} from '../actions/authActions';
import SignUp from '../Components/SignUp';
import setAuthToken from '../util/setAuthToken';
import AboutUs from '../containers/AboutUs';
import store from '../store';
import {connect} from "react-redux";
// check for TOKEN
if (localStorage.jwtToken) {
// Set auth token header auth
setAuthToken(localStorage.jwtToken);
// Decode token and get user info and exp
const decoded = jwt_decode(localStorage.jwtToken);
// Set user and isAuthenticated
store.dispatch(setCurrentUser(decoded));
// Check for expired token
const currentTime = Date.now() / 1000;
if (decoded.exp < currentTime) {
// Logout user
store.dispatch(logoutUser());
// Redirect to login
window.location.href = '/login';
}
}
class HomePage extends Component
{
componentDidMount(){
console.log(this.props.auth.isAuthenticated);
}
render(){
var {
isAuthenticated
} = this.props.auth;
return(
<div>
<Router>
<div>
<Switch>
{this.props.auth.isAuthenticated===false ? (
<div>
<Route exact path='/' component={LogoutHome}/>
<Route path='/Finance/Login' component={SignIn}/>
<Route path='/Finance/AboutUs' component={AboutUs}/>
<Route path='/Finance/ContactUs' component={ContactUs}/>
<Route path='/Finance/SignUp' component={SignUp}/>
<Route path='/Finance/Forgotpassword' component={Forgotpassword}/>
</div>
) : (
<div>
<Route path='/Finance/Home' component={Home}/>
</div>
)}
</Switch>
</div>
</Router>
</div>
);
}
}
function mapStateToProps(state){
return{
auth : state.auth
};
}
export default connect(mapStateToProps)(HomePage);
警告是这样的:
index.js:2178 Warning: React does not recognize the `computedMatch` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `computedmatch` instead. If you accidentally passed it from a parent component, remove it from the DOM element.
in div (at Homepage.js:50)
in Switch (at Homepage.js:48)
in div (at Homepage.js:47)
in Router (created by BrowserRouter)
in BrowserRouter (at Homepage.js:46)
in div (at Homepage.js:45)
in HomePage (created by Connect(HomePage))
in Connect(HomePage) (at App.js:10)
in div (at App.js:9)
in App (at index.js:10)
in Provider (at index.js:9)
我不明白天气是案例问题还是什么,因为我在很多地方都读过它,但得到的答案却不同。上面粘贴的只是主页组件。
【问题讨论】:
-
显示
Homepage组件。 -
上面发布的只是主页组件。请检查更新的问题。
-
好的.. 阅读此blog。它有你的答案.. :) 那些
<div>是<Switch>内部的罪魁祸首,使用Fragment来解决问题。 -
开关上面不需要。也在 Switch 内部。@RaviTeja 他需要一些东西,但不是
<div>或任何html dom。但<>或<Fragement>会解决这个问题。
标签: node.js reactjs redux react-redux