0

I get this error: 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

When clicking this link:

<li><NavLink activeClassName="active" to={{pathname: '/add-new'}}>Add new Recipe</NavLink>{' '} </li>

My route looks like this:

<PrivateRoute 
  exact 
  path='/add-new' 
  render={(props) => 
    <NewRecipe 
      {...props} 
      addRecipe={this.addRecipe}/>
  } 
  authenticated={authenticated} 
/>

PrivateRoute.js looks like this:

import React from "react";
import { Route, Redirect } from "react-router-dom";

export default function PrivateRoute({
  component: Component,
  authenticated,
  ...rest
}) {
  console.log(...rest);
  return (
    <Route
      {...rest}
      render={props =>
        authenticated === true ? (
          <Component {...props} {...rest} />
        ) : (
          <Redirect to="/login" />
        )
      }
    />
  );
}

Error occurs since I switched from Component={NewRecipe} to Render={...} since I need to pass a function.

1 Answer 1

1

PrivateRoute skips render props (instead of call it), fix might be something like this (notice render() logic):

export default function PrivateRoute({
  component: Component,
  authenticated,
  render,
  ...rest
}) {
return (
    <Route
      {...rest}
      render={props =>
        authenticated === true ? (
          render ? render(props) : <Component {...props} {...rest} />
        ) : (
          <Redirect to="/login" />
        )
      }
    />
  );

It's overcomplex a bit, but I hope can help you understand the idea on how render prop might be caught.

Other solution is to change /add-new to be passed as a component:

  <PrivateRoute 
    exact 
    path='/add-new' 
    addRecipe={this.addRecipe}
    component={NewRecipe}
    authenticated={authenticated} 
  />
Sign up to request clarification or add additional context in comments.

4 Comments

I used to pass it as a component but since I use Link to call PrivateRoute I need to pass the function in Render={}
@indiehjaerta yeah, np, try to update PrivateRoute as I suggested
It get's rid of the error but I think somehow it ain't passing the render correctly cause when it calls addRecipe that is in my App.js this.state is undfined which it is not.
@indiehjaerta maybe you've missed to bind the addRecipe to correct context?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.