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'));
In Nodejs, to use a variable or a function in another file, you have to export them. And we have two type export.
// Export a variable
export const App = () => { ... }
// Import App in another file
import { App } from '...'
// 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With