The most recent version of React has a hook called useId that generates a unique ID for you. You can use this hook to generate a unique ID for your label and input elements for example. This will ensure that the label and input elements are associated with each other.
Using the useId hook from React
Using Math.random to generate a unique ID
Other solution case you use a old version of React, you can use a browser API (e.g the Math API) to generate a unique ID. This will ensure that the label and input elements are associated with each other.
Using the crypto API to generate a unique ID
You can also use the crypto API:
Creating you own version of useId hook
If you're a library author, and support older React version, you can use a custom version of the useId hook.