javascript - this.props 不是函数
问题描述
我正在尝试this.state.user
使用函数传递给道具,isAuthed
但它返回
this.props.isAuthed 不是函数
. 我的最终目标是能够this.state.user
作为道具传递给我的App.js ,以根据用户是否登录,使退出按钮出现和消失。可能是什么问题?
import React from "react";
import { Redirect } from "react-router-dom";
import firebaseConfig from "./firebaseconfig";
class Dashboard extends React.Component {
constructor(props) {
super(props);
this.state = {
};
this.displayCredentials = this.displayCredentials.bind(this);
this.isAuthed = this.isAuthed.bind(this);
}
displayCredentials() {
firebaseConfig.auth().onAuthStateChanged( (user) => {
if (user) {
//console.log(user.displayName);
this.setState({
user: user.displayName
});
} else {
this.setState({
user: false
})
}
});
}
isAuthed() {
this.props.isAuthed(this.state.user);
}
componentDidMount() {
this.displayCredentials();
this.isAuthed();
}
render() {
if (this.state.user === false) {
return <Redirect to='/sign-in' />
}
return (<h1>Hello, {this.state.user}</h1>);
}
}
export default Dashboard;
IsAuthed
在父 App.js 组件中看起来像这样:
<Route path='/dashboard' component={Dashboard} isAuthed = {this.checkIfAuthed}>
它调用函数checkIfAuthed
:
checkIfAuthed(isLoggedIn){
if(isLoggedIn) {
this.setState({
isLoggedIn: true
})
}
alert('Logged In')
}
整个App.js
组件如下所示:
import React from 'react';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import './App.css';
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import Login from "./Login";
import SignUp from "./Signup";
import Forgotpassword from './Forgotpassword';
import Dashboard from './Dashboard';
class App extends React.Component {
constructor(props){
super(props);
this.state = {
isLoggedIn: false
}
this.checkIfAuthed = this.checkIfAuthed.bind(this);
}
checkIfAuthed(isLoggedIn){
if(isLoggedIn) {
this.setState({
isLoggedIn: true
})
}
alert('Logged In')
}
render() {
return (
<Router>
<div className="App">
<nav className="navbar navbar-expand-lg navbar-light fixed-top">
<div className="container">
<div className="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul className="navbar-nav ml-auto">
<li className="nav-item">
<Link className="nav-link" to={"/sign-in"}>Login</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to={"/sign-up"}>Sign up</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to={"/sign-in"}>Sign out</Link>
</li>
</ul>
</div>
</div>
</nav>
<div className="auth-wrapper">
<div className="auth-inner">
<Switch>
<Route exact path='/' component={Login} />
<Route path="/sign-in" component={Login} />
<Route path="/sign-up" component={SignUp} />
<Route path='/forgotpassword' component={Forgotpassword} />
<Route path='/dashboard' component={Dashboard} isAuthed = {this.checkIfAuthed}>
</Route>
</Switch>
</div>
</div>
</div>
</Router>
);
}
}
export default App;
解决方案
您似乎正在使用 react-router-dom 并尝试将额外的道具传递给由组件呈现的Route
组件。额外的道具不会被传递,它们会被忽略。
您可以使用该render
道具并将任何额外的道具与路线道具一起传递。
<Route
path='/dashboard'
render={routeProps => (
<Dashboard {...routeProps} isAuthed={this.checkIfAuthed} />
)}
/>
或者如果您不需要路由道具,则呈现为子组件
<Route path='/dashboard' >
<Dashboard isAuthed={this.checkIfAuthed} />
</Route>
推荐阅读
- python - Shell 脚本 + Python + 树莓派
- javascript - React Native 文件访问 api:blob.data 在 android 上是“未定义”
- python - Python:哪些类型支持弱引用?
- python - 如何制作一个可以处理单个输入或输入列表的函数
- android - 如何知道手机何时连接到 WiFi 网络
- powershell - 多个-ne过滤器powershell
- r - 如何抑制 #> 由拼凑而成的 plot_layout() 输出
- c++ - 如何从 C++ 处理 C 库中不同大小的类型
- sql - CASE 语句仍然返回多行
- python - 使所有用户都可以访问由 pip 安装的程序 (aws cli)