我正在尝试设置Firebase身份验证。我有两个组成部分。 第一个是使用firebase.auth()。currentUser的应用程序,我正在检查是否有已登录的用户,此后,如果有这样的用户,则呈现页面;如果没有,则呈现登录页面。 第二个组件是我从父级获取处理程序并尝试呈现登录页面。 现在,我只看到空白页,并且显示错误“未定义'props'”。 请帮我修复它。
App.js
import React, {Component} from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import { Layout } from 'antd';
import AddStock from './components/stocksDB/addStock';
import { Typography, Menu } from 'antd';
import 'antd/dist/antd.css';
import './App.css';
import AddClient from './components/clients/addClient.js';
import PifForm from './components/clients/pif/pifForm';
import KuaForm from './components/clients/pif/kuaForm';
import firebase from './firebase.js';
import {Login} from './components/login.js'
const { Title } = Typography;
const {Header, Footer, Content} = Layout;
class App extends Component {
constructor(props){
super(props);
this.state = {
login: '',
password: ''
}
}
handleChange = (event) => {
const target = event.target;
const name = target.name;
this.setState({
[name]: target.value
}, () => {console.log(name, this.state)})
};
signUp = () => {
firebase.auth().createUserWithEmailAndPassword(this.state.login, this.state.password).catch(function(error) {
console.log(error);
})
};
signIn = () => {
firebase.auth().signInWithEmailAndPassword(this.state.login, this.state.password).catch(function(error) {
console.log(error);
})
};
onFinishFailed = errorInfo => {
console.log('Failed:', errorInfo);
};
render() {
let user = firebase.auth().currentUser;
if (user) {
return (
<Router>
<Layout theme='dark'>
<Header mode="horizontal">
<div className='header-wrapper'>
<Title level={2} style={{"color": "rgb(166 173 180)"}}>Оберіть функцію:</Title>
<Menu theme="dark" mode="horizontal" defaultSelectedKeys={['2']}>
<Menu.Item key="1">
<Link to='/stocksList'>Довідник ЦП</Link>
</Menu.Item>
<Menu.Item key="2">
<Link to='/addClients'>Довідник клієнтів</Link>
</Menu.Item>
</Menu>
</div>
</Header>
<Content>
<Switch>
<Route path="/stocksList">
<AddStock/>
</Route>
<Route path="/addClients">
<AddClient/>
</Route>
<Route path="/kuaForm">
<KuaForm/>
</Route>
<Route path="/pifForm">
<PifForm/>
</Route>
</Switch>
</Content>
<Footer>Footer</Footer>
</Layout>
</Router>
)
} else {
return (
<Login signIn={this.signIn} onFinishaedFailed={this.onFinishFailed} handleChange={this.handleChange} signUp={this.signUp} />
)
}
}
}
export default App;
Login.js
import React from 'react';
import { Form, Input, Button } from 'antd';
import { Typography } from 'antd';
const { Title } = Typography;
export const Login = () => {
const layout = {
labelCol: { span: 8 },
wrapperCol: { span: 16 },
};
const tailLayout = {
wrapperCol: { offset: 8, span: 16 },
};
return (
<div>
<Title level={2} style={{ "color": "rgb(166 173 180)" }}>Для продовження роботи пройдіть автентифікацію.</Title>
<Form
{...layout}
name="basic"
initialValues={{ remember: true }}
onFinish={props.signIn()}
onFinishFailed={this.props.onFinishFailed()}
>
<Form.Item
label="Username"
name="username"
rules={[{ required: true, message: 'Please input your username!' }]}
>
<Input name='login' onChange={this.props.handleChange()}/>
</Form.Item>
<Form.Item
label="Password"
name="password"
rules={[{ required: true, message: 'Please input your password!' }]}
>
<Input.Password name='password' onChange={this.props.handleChange()}/>
</Form.Item>
<Form.Item {...tailLayout}>
<Button type="primary" htmlType="submit">
Увійти
</Button>
<Button type="primary" htmlType="button" onClick={this.props.signUp()}>
Зареєструватися
</Button>
</Form.Item>
</Form>
</div>
);
};
答案 0 :(得分:2)
由于Login
是一个功能组件,因此您需要传递并定义以下道具:const Login = (props) => {
。然后,您可以使用props.handleChange
和props.signUp
访问道具。
无需像在基于类的组件中那样使用this
,因为在功能组件内部未定义this
。
答案 1 :(得分:1)
您正在调用道具,但尚未在功能组件中对其进行定义。
export const Login = (props) => {
...
}