[英]Cant use uuid4 in react-native
i am trying to use uuidv4 its show a error我正在尝试使用 uuidv4 它显示错误
first iam install uuid using this command npm i uuidv4
after i am import {v4} from 'uuid';首先 iam 使用此命令安装 uuid
npm i uuidv4
在我从 'uuid' 导入 {v4} 之后; to app.js到 app.js
This is my error这是我的错误
TypeError: (0,_uuid.uuid) is not a function. (in'(0,_uuid.uuid)()','(0,_uuid.uuid)' is undefined)
this is my code这是我的代码
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;
You have to use uuid
package and react-native-get-random-values
before the uuid
import.在
uuid
导入之前,您必须使用uuid
package 和react-native-get-random-values
。 uuid
package has a V4 implementation. uuid
package 具有 V4 实现。
Check the documentation under UUID > React Native :检查UUID > React Native下的文档:
React Native
反应原生
- Install
react-native-get-random-values
安装
react-native-get-random-values
- Import it before
uuid
:在
uuid
之前导入它:import 'react-native-get-random-values'; import { v4 as uuidv4 } from 'uuid';
Use import {uuid} from 'uuidv4'
.使用
import {uuid} from 'uuidv4'
。 The mistake is in your import;错误在于您的导入;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.