J M*_*oss 9 javascript uuid reactjs create-react-app
我的 React 应用程序成功显示按钮,但收到此错误。
index.js:1const uuidv4 = require('uuid/v4');从 uuid@7.x 开始,不推荐使用深度要求喜欢。使用 Node.js CommonJS 模块时需要顶层模块,浏览器绑定时请使用 ECMAScript 模块
import React, { Component } from 'react';
import { Container, ListGroup, ListGroupItem, Button } from 'reactstrap';
import { CSSTransition, TransitionGroup } from 'react-transition-group';
import uuid from 'uuid/v4';
class ShoppingList extends Component {
state = {
items: [
{ id: uuid(), name: 'Eggs' },
{ id: uuid(), name: 'Milk' },
{ id: uuid(), name: 'Steak' },
{ id: uuid(), name: 'Water' },
]
}
render() {
const { items } = this.state;
return (
<Container>
<Button
color="dark"
style={{marginBottom: '2rem'}}
onClick={() => {
const name = prompt('Enter Item');
if (name) {
this.setState(state => ({
items: [...state.items, { id: uuid(), name }]
}));
}
}}
>Add Item</Button>
</Container>
);
}
}
export default ShoppingList;
Run Code Online (Sandbox Code Playgroud)
Gop*_*ine 16
这在最近更新库后发生了变化,现在您可以根据库描述导入 uuid:
“从 uuid@7 开始,这个库现在提供 ECMAScript 模块构建,它允许像 Webpack 和 Rollup 这样的打包程序进行“摇树”以删除死代码。相反,使用导入”
import { v4 as uuid_v4 } from "uuid";
uuid_v4()
Run Code Online (Sandbox Code Playgroud)
...或对于 CommonJS:
const { v4: uuid_v4 } = require('uuid');
uuid_v4();
Run Code Online (Sandbox Code Playgroud)