Skip to content
Myr

Improve RN example

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Andrey Sitnik · committed by Andrey Sitnik · parent 98d8d31 · Browse files · 1 file+12 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+12 added−18 removed

  • other:+12 added−18 removed

1 file+12 added−18 removed

modified:README.md+12 added-18 removed
122 unmodified lines
123in our [ID collision probability] calculator.124125[ID collision probability]: https://zelark.github.io/nano-id-cc/126127128### React Native129129To generate secure random IDs in React Native, you must use a native130random generator like [react-native-securerandom](https://www.npmjs.com/package/react-native-securerandom). Since `react-native-securerandom`131generates its random bytes on the native side, your React Native JS code must communicate with it asynchronously over the JS bridge. For example, as follows:130To generate secure random IDs in React Native, you must use131[a native random generator] like and asynchronous API:132133```js134import { generateSecureRandom } from 'react-native-securerandom';135import format from 'nanoid/async/format';136import url from 'nanoid/url';134const generateSecureRandom = require('react-native-securerandom').generateSecureRandom135const format = require('nanoid/async/format')136const url = require('nanoid/url')137138// Using Promises139format(generateSecureRandom, url, 21).then(id => {140 // id is a random ID141 console.log('random ID is', id); //=> xnCahQadzHuI1QjcbYHBE142});143144// Using async/await145async function useRandomID() {146 // id is a random ID147 const id = await format(generateSecureRandom, url, 21);148 console.log('random ID is', id); //=> A6WZ9Kz6961u0Wc8h22O1138async function createUser () {139 user.id = await format(generateSecureRandom, url, 21);140}141```142143[a native random generator]: https://github.com/rh389/react-native-securerandom144145146### Web Workers147148Web Workers don’t have access to a secure random generator.149150Security is important in IDs, when IDs should be unpredictable. For instance,151in “access by URL” link generation.152159If you don’t need unpredictable IDs, but you need Web Workers support, 153If you don’t need unpredictable IDs, but you need Web Workers support,154you can use non‑secure ID generator.155156```js157const nanoid = require('nanoid/non-secure')
132 unmodified lines