Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

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

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>
    )
}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...