Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k 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

question from:https://stackoverflow.com/questions/65602457/typeerror-react-webpack-imported-module-1-default-is-not-a-function-or-its-r

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

1 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...