This content originally appeared on DEV Community and was authored by SnowBit
Hello ?,
Today, you will learn to fetch random joke from a node module in React JS
Let's get started,
Create React App
npx create-react-app joke-boi
Install this node module
npm i one-liner-joke
Now create App.js
import React from "react";
import Joke from './Joke'
class App extends React.Component{
render(){
return(
<div>
<Joke/>
</div>
)
}
}
export default App;
Now we'll create a component called Joke in Joke.js
import React from "react";
import 'bootstrap/dist/css/bootstrap.min.css';
import './Joke.css';
import {Card} from 'react-bootstrap'
const oneLinerJoke = require('one-liner-joke');
class Joke extends React.Component{
componentDidMount() {
window.addEventListener('load', this.handleGet);
}
componentWillUnmount() {
window.removeEventListener('load', this.handleGet)
}
handleGet(){
const getRandomJoke = oneLinerJoke.getRandomJoke();
document.getElementById('joke').innerText = getRandomJoke.body;
}
render(){
return(
<div className="container">
{/* <input type="text" className="form-control" id="joke" disabled/> */}
<Card>
<Card.Body>
<h2 id="joke">Try refreshing the page if you can't find the joke. Note: It is not a joke</h2><br /><hr />
<button className="btn btn-info nextBtn" onClick={this.handleGet}>Don't click here for next joke ?</button>
</Card.Body>
</Card>
</div>
)
}
}
export default Joke;
And all set! now you can run in console
npm start
Check out GitHub Repository and fork
Happy Coding!
This content originally appeared on DEV Community and was authored by SnowBit
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.

APA
MLA
SnowBit | Sciencx (2021-09-20T05:45:22+00:00) Random Joke Fetching in React. Retrieved from https://www.scien.cx/2021/09/20/random-joke-fetching-in-react/
" » Random Joke Fetching in React." SnowBit | Sciencx - Monday September 20, 2021, https://www.scien.cx/2021/09/20/random-joke-fetching-in-react/
HARVARDSnowBit | Sciencx Monday September 20, 2021 » Random Joke Fetching in React., viewed ,<https://www.scien.cx/2021/09/20/random-joke-fetching-in-react/>
VANCOUVERSnowBit | Sciencx - » Random Joke Fetching in React. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/20/random-joke-fetching-in-react/
CHICAGO" » Random Joke Fetching in React." SnowBit | Sciencx - Accessed . https://www.scien.cx/2021/09/20/random-joke-fetching-in-react/
IEEE" » Random Joke Fetching in React." SnowBit | Sciencx [Online]. Available: https://www.scien.cx/2021/09/20/random-joke-fetching-in-react/. [Accessed: ]
rf:citation » Random Joke Fetching in React | SnowBit | Sciencx | https://www.scien.cx/2021/09/20/random-joke-fetching-in-react/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.