0

I can't figure out how to import a extremely simple variable from one file into another (new to react). I have the main App.js where line 3 appears to be the problem:

import React, {Component} from "react";
import NavMenu from "./components/nav-menu";
import navLinks from "/components/nav-links";

export default class App extends Component {
  constructor() {
    super();

    this.state = {
      navLinks: navLinks
    }
  }

  render() {
    return (
      <div id="container">
        <NavMenu navLinks={this.state.navLinks} />
      </div>
    );
  } 
}

Which just imports (or should import) the navLinks variable from this nav-links.js file:

import React from 'react';

export const navLinks = [
    {
      name: "home",
      href: "/"
    },
    {
      name: "subs",
      href: "/subs"
    }
  ];

Can someone point me in the right direction of where I'm going wrong here? Also, it's being used to just render a navigation menu (so links within a tags) - is this generally the right way to go about adding nav links?

Stackblitz: https://stackblitz.com/edit/react-j42gep

2 Answers 2

1

If you are doing this:

export const navLinks = [
    {
      name: "home",
      href: "/"
    },
    {
      name: "subs",
      href: "/subs"
    }
  ];

then you are importing it wrong. Then do this:

import {navLinks} from "./components/nav-links";

Note this: ./components/nav-links you forgot ./ in import statement

If you dont want to use {} the add default statement in your export like this:

const navLinks = [
    {
      name: "home",
      href: "/"
    },
    {
      name: "subs",
      href: "/subs"
    }
  ];

  export default navLinks;
Sign up to request clarification or add additional context in comments.

Comments

1

You're missing to export the nav links as default :

import React from 'react';

 const navLinks = [
    {
      name: "home",
      href: "/"
    },
    {
      name: "subs",
      href: "/subs"
    }
  ];

  export default navLinks;

and add dot . in import navLinks from "./components/nav-links";

2 Comments

still not working with this error: Module not found: You attempted to import /components/nav-links which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
you're missing also . in import navLinks from "./components/nav-links";

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.