亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React:從 uuid 開始不推薦使用深度要求,請要求頂級模塊

React:從 uuid 開始不推薦使用深度要求,請要求頂級模塊

慕碼人8056858 2022-05-22 16:01:45
我的 React 應用程序成功顯示按鈕,但收到此錯誤。index.js:1const uuidv4 = require('uuid/v4');從 [email protected] 開始,不推薦使用需要 like 的深度。使用 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;我嘗試使用 'import { v4 as uuidv4 } from 'uuid';uuidv4();'但是我的按鈕不會出現,我會得到錯誤:未捕獲的 ReferenceError:未定義 uuid也許我應該得到這個錯誤?目前一切正常嗎?
查看完整描述

2 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

這在最近更新庫后發生了變化,現在您可以根據庫描述導入 uuid:


“從 uuid@7 開始,這個庫現在提供 ECMAScript 模塊構建,它允許像 Webpack 和 Rollup 這樣的打包器進行“tree-shaking”以刪除死代碼。相反,使用導入”


import { v4 as uuid_v4 } from "uuid";

uuid_v4()

...或者對于 CommonJS:


const { v4: uuid_v4 } = require('uuid');

uuid_v4();


查看完整回答
反對 回復 2022-05-22
?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

您可以改用react -uuid

npm i react-uuid

用法

import uuid from "react-uuid";


查看完整回答
反對 回復 2022-05-22
  • 2 回答
  • 0 關注
  • 250 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號