4
I am making an application in React.js and gave this problem:
Element type is invalid: expected a string (for built-in Components) or a class/Function (for Composite Components) but got: Undefined. You likely forgot to export your Component from the file it’s defined in, or you Might have Mixed up default and named Imports.
import React, { Component } from "react";
import { Link } from "react-router-dom";
import Logo from "../../imagens/logo.png";
import { Form, Container } from "./styles";
class SignUp extends Component {
state = {
username: "",
email: "",
password: "",
error: ""
};
handleSignUp = e => {
e.preventDefault();
alert("Eu vou te registrar");
};
render() {
return (
<Container>
<Form onSubmit={this.handleSignUp}>
<img src={Logo} alt="Airbnb logo" />
{this.state.error && <p>{this.state.error}</p>}
<input
type="text"
placeholder="Nome de usuário"
onChange={e => this.setState({ username: e.target.value })}
/>
<input
type="email"
placeholder="Endereço de e-mail"
onChange={e => this.setState({ email: e.target.value })}
/>
<input
type="password"
placeholder="Senha"
onChange={e => this.setState({ password: e.target.value })}
/>
<button type="submit">Cadastrar grátis</button>
<hr />
<Link to="/">Fazer login</Link>
</Form>
</Container>
);
}
}
export default SignUp;
import React from "react";
import { BrowserRouter, Route, Switch, Redirect } from "react-router-dom";
import SignUp from "../components/SignUp";
import { isAuthenticated } from "./auth";
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props =>
isAuthenticated() ? (
<Component {...props} />
) : (
<Redirect to={{ pathname: "/", state: { from: props.location } }} />
)
}
/>
);
const Routes = () => (
<BrowserRouter>
<Switch>
<Route exact path="/" component={() => <h1>Login</h1>} />
<Route path="/signup" component={SignUp} />
<PrivateRoute path="/app" component={() => <h1>App</h1>} />
<Route path="*" component={() => <h1>Page not found</h1>} />
</Switch>
</BrowserRouter>
);
export default Routes;
by error message it informs that the problem is in the method
SignUp
, you could post this method?– William
@William Postei
– Thales Vinicius
I can’t see the image (locked here). Add as text the code: of the class you want to import, from what Willam told Signup; the code of the class that imports the first.
– mari
And how Signup is imported where it is being used?
– mari
@Mari is being imported in the Routes.js
– Thales Vinicius
But it’s important how. Put index.js (or at least the start where the Imports are).
– mari
@But I put!
– Thales Vinicius
@Thales-Vinicius import your component
SignUp
with the keys"{}"
, for example{ SignUp }
– William
Sorry! Nothing wrong with Signup import. When you give this message "You likely forgot to export your Component..." what usually happens is that you have not exported it as default and try to import as if it were default (i.e. without the keys). In short: if you export with 'export default Signup' you import without keys 'import Signup from './signup'; if you export 'export Signup', you import with keys 'import { Signup } from './signup'.
– mari
I saw the image on the phone, he says to watch the Signup render(), then the Signup he is importing ok. Check if: Form or Container are not exported with 'export default'. Import image I do not know.
– mari
@William didn’t turn out well
– Thales Vinicius
@But the form is imported right, already the container I do not understand what you are talking about hehe
– Thales Vinicius
In the Styles file you will get the declaration of Form and Container. If for example it looks like this: 'export default Form;' and 'export Container' (of course, it can only have 1 default), you would need to import it like this in Signup: import Form, {Container} from './Styles';
– mari
If both are just like export form and export container then import is ok. In that case I would try to take the image and import of it to test.
– mari
@Mari was import error in Styles! Thank you!!
– Thales Vinicius
Good! Then I’ll put as an answer and you accept =)
– mari