Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REACT - Error: Attempted import error/ App' does not contain a default export (imported as 'App'

Returns an error: ./src/index.js Attempted import error: './App' does not contain a default export (imported as 'App').

import React, { Component, useState } from "react";

const App = () => {
    const [count, setCount] = useState(0);

  const  increment = () => {
        setCount(count + 1);
    };

    return (
      <div>
          <h2> counter app </h2>
          <button onClick={increment}>Clicked {count} times</button>
      </div>
    );
};

index

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';


ReactDOM.render(<App />, document.getElementById('root'));
like image 364
Omar Avatar asked Dec 18 '22 15:12

Omar


1 Answers

In Nodejs, to use a variable or a function in another file, you have to export them. And we have two type export.

1. Use export const

// Export a variable
export const App = () => { ... }

// Import App in another file
import { App } from '...'

2. Use export default

// Export default
const App = () => { ... }
export default App

// Import App in another file
import App from '...'

Follow my example and see your code. You missing export App to could use this variable in another file.

So, In your case, you must export App to use in index.js:

import React, { Component, useState } from "react";

const App = () => {
    const [count, setCount] = useState(0);

  const  increment = () => {
        setCount(count + 1);
    };

    return (
      <div>
          <h2> counter app </h2>
          <button onClick={increment}>Clicked {count} times</button>
      </div>
    );
};
export default App

Remember, you just only have one export default in one file.

like image 161
aldenn Avatar answered Dec 31 '22 01:12

aldenn