Formik Element type is invalid

半城伤御伤魂 提交于 2019-12-11 01:57:29

问题


Formik

I am new to formik and start using it every time i start learning i got this issue

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

I am unable to locate error cause this is my code // here is Login.js file

    import React, { Component } from "react";
    import { Row, Col, Button } from "react-bootstrap";
    import validator from "validator";
    import { withFormik, Form, Field } from 'formik';
    import Yup from 'yup';
    import { RingLoader } from "react-spinners";
    import "./Login.css";
    import logo from "../../img/logo.png";
    var NotificationSystem = require("react-notification-system");


    class Login extends Component {

      render() {

        let { errors, touched } = this.props;

        console.log("Login Component",this.props);
        return (
          <div className="content">
            <Row>
              <Col md={4} mdOffset={4} xs={10} xsOffset={1}>
                <div style={stylelogobox}>
                  <img src={logo} style={logoStyle} />
                </div>
                <Form className="form-horizontal">
        <fieldset>
          <legend className="text-center">
            <h2>Login</h2>
          </legend>

          <div className="form-group">
            <label className="col-lg-2 control-label">Username</label>
            <div className="col-lg-10">
              <Field
                type="text"
                className="form-control"
                placeholder="username"
                name="username"
              />
              {errors.username && touched.username && errors.username}
            </div>
          </div>

          <div className="form-group">
            <label htmlFor="inputPassword" className="col-lg-2 control-label">
              Password
            </label>
            <div className="col-lg-10">
              <Field
                type="password"
                className="form-control"
                placeholder="Password"
                name="password"
              />
              {errors.password && touched.password && errors.password}
            </div>
          </div>
          <div className="form-group">
            <Col lg={10} lgOffset={2}>
              <button
                // disabled={this.props.user.isLogging}
                className="btn btn-primary login-button btn-block LoginButton"
                // disabled={this.state.isEnabled}
                // onClick={this.handlesOnLogin}
              >
                <span> Login </span>
              </button>
            </Col>
          </div>
        </fieldset>
      </Form>

              </Col>
            </Row>
            <Row>
              <Col md={4} mdOffset={4} xs={10} xsOffset={1}>
                <div className="pull-right">
                  <Button bsStyle="default" onClick={this.goForgetPassword}>
                    Password Reset
                  </Button>
                </div>
              </Col>
            </Row>

            <div className="sweet-loading loadingSpinner">
              <RingLoader
                color={"#5c1a3e"}
                //loading={this.state.loading}
                loading={this.props.user.isLogging}
              />
            </div>

            <div>
              <NotificationSystem ref="notificationSystem" />
            </div>
          </div>
        );
      }




const formikEnhancer = withFormik({
  initialValues: { username: "", password: "" },
  handleSubmit: (values, { setSubmitting }) => {
    setTimeout(() => {
      alert(JSON.stringify(values, null, 2));
      setSubmitting(false);
    }, 400);
  }
});

export default formikEnhancer(Login);

I am importing this file to index.js where i am connecting this to redux store. index.js and login.js are in same folder

here is Index.js

import { connect } from "react-redux";
import Login from "./Login";


import {
  login,
  loginRequestLoader,
  resetInvaliduser
} from "../../redux/actions/user";

const mapStateToProps = state => {
  return {

    user: state.user,
    loginFlag: state.user.login
  };
};

const mapDispatchToProps = dispatch => {
  return {
    loginRequestLoader: () => dispatch(loginRequestLoader()),
    login: (username, password) => dispatch(login(username, password)),
    resetInvaliduser: () => dispatch(resetInvaliduser())
  };
};

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Login);

here is Import from index.js file into Routes file

import Login                    from './views/login/index';

i don't know why this is getting the error i have tries withFormik and also Formik component but nothing work i also tries to see props in Login component using console log but it is also not logging the props means nothing is log.


回答1:


This is a generic error caused by issues with imports. However, from my tests with react@16, it looks like any version below react-dom@16.4 will show this specific error where the element is imported as an object.

This CodeSandbox demonstrates the error: https://codesandbox.io/s/0vrn83o6ww (Change the react/react-dom versions to test your environment)

If you're using react@16, update react-dom to a version >=16.4 and this should fix the error.

Recent versions formik with incompatible versions of react-dom have made this error come up: https://github.com/jaredpalmer/formik/issues/907




回答2:


As @brettinternet said, it's generic, and common. Could be that when using export default formikEnhancer(Login), then you need to import it like a non-default class:

import {Login} from './Login';

For other folks searching, it's also happens when you entirely forget to define your component's default export.



来源:https://stackoverflow.com/questions/50271471/formik-element-type-is-invalid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!