0

I am using react-cookie in my project. When i use withCookies() i am getting warning in console log like this

Warning: Failed prop type: Invalid prop component of type object supplied to Route, expected function

How can i solve this.?

Below is the code

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 };
Sudeep Kumar
  • 839
  • 1
  • 7
  • 16
  • The warning concerns an instance of a `Route` component, meanwhile, in your code, there is no such component. Could you please include it ? – Incepter Feb 26 '19 at 10:18
  • `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
    ; } } export default withCookies(App);`
    – Sudeep Kumar Feb 26 '19 at 10:27
  • This is my App.js file code. In this file i have added routing code @MohamedELAYADI – Sudeep Kumar Feb 26 '19 at 10:28
  • Can you please show your `pages.js` file ? – Incepter Feb 26 '19 at 10:52
  • pages is the directory. In that directory we have all page components with one index.js file. That index.js file consist the below code. `export * from './login/Login'; export * from './signup/Signup';` – Sudeep Kumar Feb 26 '19 at 11:10
  • That's it, i will write a possible answer for your issue, please note that this has nothing to do with `withCookies`. – Incepter Feb 26 '19 at 11:19

1 Answers1

1

Following the comments, you are exporting all imports from your containers' files as default, so basically you'll get an object, not a function(the exact export).

To prevent this, there is several ways to do it:

  • Import your container in App from its own file.
  • in index.js of pages, you can do this :
import { Login } from `LoginContainerFile`;
import { Comp1} from `Comp1ContainerFile`;
import { Comp2} from `Comp2ContainerFile`;
export { Login, Comp1, Comp2 };

Good luck and hope it resolves your problem.

Incepter
  • 2,711
  • 15
  • 33