Reputation: 2970
I have a place wherein I am required to use npm uuid package for generating unique Id's. After installing uuid package, the usage is as follows:
const uuid = require('uuid/v1');
uuid();
But I have error which says:
[eslint] Unexpected require(). (global-require)
My function is as below:
someFunction = (i, event) => {
if(someCondition) {
//generate some unique id
const uuid1 = require('uuid/v1');
uuid1();
//call some function and pass this id
someFunction2(uuid1);
} else{
//generate some unique id
const uuid2 = require('uuid/v1');
uuid2();
//call some function and pass this id
someFunction2(uuid2);
}
What is the best way to use require in ReactJs.
Upvotes: 36
Views: 98798
Reputation: 87
using in a typescript app, you must install with types init.
npm i --save-dev @types/uuid
Then Importing like below will work with ease
import { v4 as uuidv4 } from "uuid";
Upvotes: 0
Reputation: 345
If what you are trying to do is to display a list of component items, you might consider this pattern:
import React from 'react'
import {v4 as uuidv4} from 'uuid'
const Project = ({title}) =>{
return(
<li>{title}</li>
)
}
const ProjectList = ({projects}) => {
return (
<div>
<ul>
{
projects.map(project => <Project title={project.title} key={uuidv4()}/>)
}
</ul>
</div>
)
}
export default ProjectList
Upvotes: 0
Reputation: 1
Generate Unique ID in JavaScript using UUID
make sure your at root run ls if your not sure look for package.json
run npm i uuid
to use it in the file you need
const {v4: uuidv4} = require('uuid')
now for the hard part if your using a route such as
controller.upload = (req, res) => {
const id = uuidv4() // each upload will have a uuid thats random add your logic
}
Upvotes: 0
Reputation: 216
Using React with typescript version 4.5.0 or higher.
Run: npm install --save @types/uuid
Create directory "@types" in root folder with file "allTypes.d.ts" and add there
declare module "uuid";
In ts.config.json under compilerOptions add:
"typeRoots": ["./node_modules/@types","./@types" ]
Rerun your app and import where necessary
import { v4 as uuidv4 } from "uuid";
Upvotes: 1
Reputation: 648
you must go to this folder => your drive:/your projectfolder/node_modules/uuid/dist/v1 and see your uuid's folder by defult this is in top folder
import UUID From 'uuid/dist/v1'
Upvotes: 0
Reputation: 923
import React from "react";
import ReactDOM from "react-dom";
import * as uuid from "uuid";
function App() {
return (
<div className="App">
<h1>{uuid.v4()}</h1>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Upvotes: 1
Reputation: 14001
As of September,2020 I got this working by first, installing the types as
yarn add @types/uuid
then as
import React from "react";
import ReactDOM from "react-dom";
import { v4 as uuidv4 } from 'uuid';
function App() {
return (
<div className="App">
<h1>{uuidv4()}</h1>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
If this code don't work in future, refer to this section of official docs.
Upvotes: 20
Reputation: 9
you can import in different ways
1----From the command line Installing
npm i react-uuid
import uuid from 'react-uuid'
and use for key variable like below
key={uuid()}
2---- A better way I always use is
first, install
npm install uuid
then import
import { v1 as uuidv1 } from 'uuid';
and use
uuidv1();
Upvotes: 0
Reputation: 590
For React JS:
import { v1 as uuidv1 } from 'uuid';
uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d'
Upvotes: 0
Reputation: 4050
Just in case you would like to use uuid v4, this is how you can import it in react.
import uuid from 'uuid/v4'
Upvotes: 2
Reputation: 1773
In react use import
.
In Nodejs project use 'require'.
If you want to use import
in normal nodejs project "babel" is needed to setup.
Upvotes: 0
Reputation: 10179
Try this:
import React from "react";
import ReactDOM from "react-dom";
import uuid from "uuid";
function App() {
return (
<div className="App">
<h1>{uuid.v4()}</h1>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
This is a working example: https://codesandbox.io/s/0pr5vz48kv
Upvotes: 58
Reputation: 543
You don't have to require the package always. Just require it only once and use it wherever you want.
const uuidv1 = require('uuid/v1');
if (condition) {
some_id = uuidv1();
some_function(some_id);
} else {
other_id = uuidv1();
other_function(other_id);
}
It's as simple as that.
Upvotes: 3