A Pen by Pushp Vashisht on CodePen.
Created
August 18, 2019 19:26
-
-
Save pushp1997/ec65ae065ae1504194611da42dcf1844 to your computer and use it in GitHub Desktop.
Random Quote Machine Using React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script> | |
<div id="root"></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const quotes = ['Be yourself; everyone else is already taken.', 'Be the change that you wish to see in the world.', 'No one can make you feel inferior without your consent.', 'Darkness cannot drive out darkness: only light can do that. Hate cannot drive out hate: only love can do that.']; | |
const authors = ['Oscar Wilde', 'Mahatma Gandhi', 'Eleanor Roosevelt', 'Martin Luther King Jr.']; | |
let ind = 0; | |
class TweetQuote extends React.Component{ | |
constructor(props){ | |
super(props); | |
} | |
render(){ | |
return ( | |
<a id="tweet-quote" href="twitter.com/intent/tweet">Tweet Quote</a> | |
); | |
} | |
}; | |
class Text extends React.Component{ | |
constructor(props){ | |
super(props); | |
} | |
render(){ | |
return ( | |
<p id="text">{quotes[this.props.ind]}</p> | |
); | |
} | |
}; | |
class Author extends React.Component{ | |
constructor(props){ | |
super(props); | |
} | |
render(){ | |
return ( | |
<p id="author">{authors[this.props.ind]}</p> | |
); | |
} | |
}; | |
class QuoteBox extends React.Component{ | |
constructor(props){ | |
super(props); | |
this.state = { | |
ind : 0 | |
}; | |
this.newQuote = this.newQuote.bind(this); | |
} | |
newQuote(){ | |
this.setState({ | |
ind : Math.floor(Math.random() * 4) | |
}); | |
} | |
render(){ | |
return ( | |
<div id="quote-box"> | |
<Text ind={this.state.ind} /> | |
<Author ind={this.state.ind} /> | |
<a href="#" id="new-quote" onClick={this.newQuote}>New Quote</a><br /> | |
<TweetQuote /> | |
</div> | |
); | |
} | |
}; | |
React.render(<QuoteBox />, document.getElementById('root')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="//cdnjs.cloudflare.com/ajax/libs/react/0.13.0/react.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#quote-box{ | |
margin: 0 auto; | |
text-align: center; | |
width: 80vw; | |
border: ridge; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment