简体   繁体   中英

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. abcd

I have an error when try get function ExpenseItem() from ExpenseItem.js to App.js This is my code

file ExpenseItem.js

function ExpenseItem() {
    return <h2>Expense item!</h2>;
}
export default ExpenseItem();

and this is app.js

import ExpenseItem from "./components/ExpenseItem.js";
function App() {
  return (
    <div> 
      <h2>hello world! </h2>
      <ExpenseItem></ExpenseItem>
    </div>
  );
}
export default App;

I expected have 2 row: "hello world!" and "Expense item" but nothing appear on display

It seems like you are calling the function in the export.

Change the ExpenseItem -file to this:

function ExpenseItem() {
    return <h2>Expense item!</h2>;
}
export default ExpenseItem; // Don't call the function here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. React-Native Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in ReactJS Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object NextJS: error - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object Invariant Violation: Element type is invalid: expected a string (built-in components) or a class/function (composite components) but got: object × Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object Webpack: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object Material UI - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object Element type is invalid: expected a string (for built-in components) or a class/function but got: object. NextJS: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM