gpt4 book ai didi

javascript - 不能在 react-native 中使用 uuid4

转载 作者:行者123 更新时间:2023-12-04 02:36:46 26 4
gpt4 key购买 nike

我正在尝试使用 uuidv4 它显示错误

首先 iam 使用此命令安装 uuid npm i uuidv4在我从 'uuid' 导入 {v4} 之后;到 app.js

这是我的错误

TypeError: (0,_uuid.uuid) is not a function. (in'(0,_uuid.uuid)()','(0,_uuid.uuid)' is undefined)

这是我的代码
import React, {useState} from 'react';
import {View, Text, StyleSheet, FlatList} from 'react-native';
import Header from './components/Header';
import {uuid} from 'uuid';

const App = () => {
const [items, setItems] = useState([
{
id: uuid(),
text: 'Milk',
},
]);

return (
<View style={styles.container}>
<Header />
<FlatList
data={items}
renderItem={({item}) => <Text>{item.text}</Text>}
/>
</View>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default App;

最佳答案

您必须使用 uuid 包和 react-native-get-random-values uuid 之前进口。 uuid 包有一个 V4 实现。

查看 UUID > React Native 下的文档:

React Native

  1. Install react-native-get-random-values
  2. Import it before uuid:
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';

关于javascript - 不能在 react-native 中使用 uuid4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61443692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com