oreolodge.blogg.se

Uuid generator react react stackoverflow
Uuid generator react react stackoverflow









  1. #Uuid generator react react stackoverflow code#
  2. #Uuid generator react react stackoverflow mac#

You will want a Random-based UUID, which is 4. The paper is not so technical, I suggest reading it: /html/rfc4122

uuid generator react react stackoverflow

If you have a counter and only 1 machine generating it, you will not need anything else anyway. You do not need a counter, as the 100s-nanoseconds interval and a good random number should be enough to avoid a collision.

#Uuid generator react react stackoverflow mac#

), and the UUID uses something from the localhost (a mac address for instance). Other tools like databases use it too (like MongoDB)īeside your parameters (time and random number), you need another one, to avoid collision between 2 separate instances that reach the same random at the same time (I know that is far fetched. package/uuid seems to respect the standard. You do not need to invent a new one, just read the specs or use the npm package: In terms of architecture Components should be as "dumb" as possible and should only depends on the state management systems, like Contexts, for example.UUID is pretty standard, and already has 5 version of algorithms. In my opinion the room object should be completely prepared and initialized (and room id set) before passing it down to the Chat component, so Chat will get a non-nullable Room object with Id set. I dont think it is possible to handle those scenarios right in this Chat component. In case the component is mounted but room is null or undefined - roomId still will be set and that would be strange to me. In case you had room without an Id, texted something there, then switched to another room without an Id without unmounting (destroying) a Chat component itself - for both those rooms without Id the randomId (roomId) will be the same.

#Uuid generator react react stackoverflow code#

will not trigger this hook (and your code in question will act in same way also)Įverything from above can be easilly replaced with useState + useEffect hooks, but useMemo is just like shorter.īut with that im a bit concerned about const roomId = room ? room.id : randomId logic overall for a few cases: Changing the room from 1 with id=null to 2 with id=0 Cons - react will raise a warning about "useMemo has a complex expression in the dependency array" // will be triggered only when room.id (string) is changed const randomId = useMemo(() => nanoid(), ) Īlso you can wrap the roomId with same useMemo hook. Dynamically generating JSX in React webpack not generating bundle. What can be done: Use the useMemo hook to get a value that is evaluated only 1 on component mount and is persistent during rerenders due to depsArray is empty.

uuid generator react react stackoverflow

As you said, "it never change after the first time". So, how can I get a new random id every time I open the chat screen?ĭeclaring a randomId in a way you did is just a normal way to have normal constants, they will not be changed until browser page reload. The problem comes when I navigate to home and try to create a new second chat, the problem is it keeps the first ID, it is like it never change after the first time. I declare nanoId before the component declaration: import "react-native-get-random-values" Įxport default function Chat( = useUserAuth() //authenticated user from the context providerĬonst roomId = room ? room.id : randomId`Īll works fine, I create the chat correctly, I can even send the messages correctly because it is kept with the same id that was used to create the chat. I have a chat app, when I create for the first time a new chat I need an random Id to identify the chat, then, when I try to send the message I need that same id.











Uuid generator react react stackoverflow