【问题标题】:Getting warning when i use withCookies()当我使用 withCookies() 时收到警告
【发布时间】:2019-02-26 10:07:52
【问题描述】:

我在我的项目中使用 react-cookie。当我使用 withCookies() 时,我在控制台日志中收到这样的警告

警告:道具类型失败:对象类型的道具组件无效 提供给 Route,预期的功能

我该如何解决?

下面是代码

import React from 'react';
import { Form, Icon, Input, Button, Checkbox } from 'antd';
import {  withCookies } from "react-cookie";
import './Login.css';

class NormalLoginForm extends React.Component {
    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                console.log(values);
                const { cookies } = this.props;
                cookies.set('token', 'dfsfsd54dg2g45fg575f432sd4');
            }
        });
    }

    render() {
        const { getFieldDecorator } = this.props.form;
        return (
            <div style={{ textAlign: 'center' }}>
                <img className="logo-white" src={'https://cdn.medcampus.io/wp-content/uploads/2018/08/01131559/MC_Logo_Black.png'} alt="logo"/>
                <div className="login-container">
                    <br/>
                    <Form onSubmit={this.handleSubmit} className="login-form">
                        <Form.Item>
                            {getFieldDecorator('email', {
                                rules: [{ required: true, message: 'Please input your Email id!' }, {
                                    type: 'email', message: 'The input is not valid E-mail!',
                                }],
                            })(
                                <Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="Email" size={"large"} />
                            )}
                        </Form.Item>
                        <Form.Item>
                            {getFieldDecorator('password', {
                                rules: [{ required: true, message: 'Please input your Password!' }],
                            })(
                                <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" size={"large"} placeholder="Password" />
                            )}
                        </Form.Item>
                        <Form.Item>
                            {getFieldDecorator('remember', {
                                valuePropName: 'checked',
                                initialValue: true,
                            })(
                                <Checkbox>Remember me</Checkbox>
                            )}
                            <a className="login-form-forgot" href="">Forgot password</a>
                            <Button type="primary" size={"large"} htmlType="submit" className="login-form-button">Log in</Button>
                        </Form.Item>
                    </Form>
                </div>
            </div>
        );
    }
}

const Login = withCookies(Form.create({ name: 'normal_login' })(NormalLoginForm));

export { Login };

【问题讨论】:

  • 警告涉及Route 组件的实例,同时,在您的代码中,没有这样的组件。你能把它包括进来吗?
  • import React, {Component} from 'react'; import {Route} from 'react-router-dom'; import { withCookies } from 'react-cookie'; import {Login, Signup} from "./pages"; import {BaseLayout} from './components'; import './App.css'; class App extends Component { render() { let { cookies } = this.props; console.log(cookies.getAll()); return &lt;div&gt; &lt;Route path="/" exact component={BaseLayout} /&gt; &lt;Route path="/login" component={Login} /&gt; &lt;/div&gt;; } } export default withCookies(App);
  • 这是我的 App.js 文件代码。在这个文件中,我添加了路由代码@MohamedELAYADI
  • 你能出示你的pages.js文件吗?
  • pages 是目录。在该目录中,我们拥有一个 index.js 文件的所有页面组件。该 index.js 文件包含以下代码。 export * from './login/Login'; export * from './signup/Signup';

标签: reactjs react-router react-cookie


【解决方案1】:

在 cmets 之后,您将从容器文件中导出所有导入作为默认值,因此基本上您将获得一个对象,而不是一个函数(确切的导出)。

为了防止这种情况,有几种方法可以做到:

  • App 自己的文件中导入您的容器。
  • pagesindex.js 中,您可以这样做:
import { Login } from `LoginContainerFile`;
import { Comp1} from `Comp1ContainerFile`;
import { Comp2} from `Comp2ContainerFile`;
export { Login, Comp1, Comp2 };

祝你好运,希望它能解决你的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 2011-04-30
    • 2019-07-17
    • 2012-06-11
    相关资源
    最近更新 更多