apostrophecms / random-words

Generate one or more common English words. Intended for use as sample text, for example generating random blog posts for testing
MIT License
249 stars 72 forks source link

Cannot return the value of randomWords without console.log #20

Closed AkhatorEnosa closed 4 years ago

AkhatorEnosa commented 4 years ago

I would love for it to output a word without me using console.log in a react app i am building. If possible, how do i go about it?

boutell commented 4 years ago

randomWords returns a word to your code, which is then responsible for doing something with it. It does not write things to React, or the console, or anywhere else. It just generates words.

The simplest thing you can do with it is pass it to console.log, but there is no restriction on what you do with it. For instance, you could write something like this in vanilla JavaScript:

document.querySelector('#some-input-element').value = randomWords();

And now the input element with that id would contain the word.

In a React component, you could use random-words to initialize a property in this.state, but this isn't a feature of random-words itself, because random-words is only responsible for its own job (returning a word to your code).