最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: react__WEBPACK_IMPORTED_MODULE_1___default is not a function or its return value is not iterable - Stack

programmeradmin3浏览0评论

I'm trying to use useState in functional component in react framework. But I am getting this error. What would be the reason?

App.js

import useState from 'react';
import Header from './components/Header';
import TodoList from './components/TodoList';
import Form from './components/Form';

function App() {

  const [todos, setTodos] = useState({ id: 1, todo: "Todo1" }, { id: 2, todo: "Todo2" });
  return (
    <div className="row justify-content-center">
      <div className="col-lg-6 col-md-7 col-9">
        <Header />
        <Form onClick={() => setTodos()} />
        <TodoList todos={todos} />

      </div>
    </div>
  );
}

export default App;

TodoList.js:

import ListItem from './TodoListItem';
export default function TodoList(props) {
    return (
        <div>
            {props.todos.map((todo) => <ListItem todo={todo} />)}
        </div>
    )
}

TodoListItem.js

import styles from '../sass/todoListItem.module.scss';
export default function TodoListItem({ todo }) {
    return (
        <div className={`card ${styles.title}`}>
            {todo}
        </div>
    )
}

I'm trying to use useState in functional component in react framework. But I am getting this error. What would be the reason?

App.js

import useState from 'react';
import Header from './components/Header';
import TodoList from './components/TodoList';
import Form from './components/Form';

function App() {

  const [todos, setTodos] = useState({ id: 1, todo: "Todo1" }, { id: 2, todo: "Todo2" });
  return (
    <div className="row justify-content-center">
      <div className="col-lg-6 col-md-7 col-9">
        <Header />
        <Form onClick={() => setTodos()} />
        <TodoList todos={todos} />

      </div>
    </div>
  );
}

export default App;

TodoList.js:

import ListItem from './TodoListItem';
export default function TodoList(props) {
    return (
        <div>
            {props.todos.map((todo) => <ListItem todo={todo} />)}
        </div>
    )
}

TodoListItem.js

import styles from '../sass/todoListItem.module.scss';
export default function TodoListItem({ todo }) {
    return (
        <div className={`card ${styles.title}`}>
            {todo}
        </div>
    )
}

Share Improve this question edited Jan 11, 2023 at 6:31 Dan Bonachea 2,4775 gold badges18 silver badges34 bronze badges asked Jan 6, 2021 at 19:46 sumeyyeyegsumeyyeyeg 931 gold badge1 silver badge4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

This is because useState is not the default export from the react package.

To import specific modules from a package you should use the curly brace syntax. Try using import { useState } from 'react';

More information on import syntax can be found here

you must add your component this :

import { useState } from 'react';

useState into {} like this : { useState }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论