Vivek v kalanka
Vivek v kalanka

Reputation: 81

Cant use uuid4 in react-native

i am trying to use uuidv4 its show a error

first iam install uuid using this command npm i uuidv4 after i am import {v4} from 'uuid'; to 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;

Upvotes: 7

Views: 6540

Answers (2)

Christos Lytras
Christos Lytras

Reputation: 37318

You have to use uuid package and react-native-get-random-values before the uuid import. uuid package has a V4 implementation.

Check the documentation under 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';

Upvotes: 16

Josh
Josh

Reputation: 841

Use import {uuid} from 'uuidv4'. The mistake is in your import;

Upvotes: -3

Related Questions