So what I’m trying to do is incorporate the color change into my generateQuote()
function (which is bound to the App component and run in the CDM function). Then, this.generateQuote
is passed into the handleNewQuote
property. So, in theory, generateQuote
should be changing:
- the quote text
- the author’s name
- the author’s picture
- the wikipedia link attributed to that author
- the component’s background color (which randomly selected from an array of colors)
For more context:
const quotes = [
{
quote: "Many hands make light work.",
author: "John Heywood",
profile: "http://www.luminarium.org/renlit/heywoodport.gif",
link: "https://en.wikipedia.org/wiki/John_Heywood"
},
{
quote: 'One person can make a difference, and everyone should try.',
author: 'John F. Kennedy',
profile: 'http://images.politico.com/global/080408_50bar_jfk.jpg',
link: "https://en.wikipedia.org/wiki/John_F._Kennedy"
},
{
quote: 'The best way to predict the future is to invent it',
author: 'Alan Kay',
profile: 'https://qph.fs.quoracdn.net/main-thumb-117344100-200-bevjprxhfxjthnblpqybhtlunpnkhvxy.jpeg',
link: "https://en.wikipedia.org/wiki/Alan_Kay"
},
{
quote: 'All we have to decide is what to do with the time that is given to us',
author: 'J.R.R. Tolkien',
profile: 'https://static9planetadelibroscom.cdnstatics.com/usuaris/autores/fotos/19/tam_1/000018687_1_Tolkien_J_201512041814._R_201512041814._R_201512041814.__200_201512041814.jpg',
link: "https://en.wikipedia.org/wiki/J._R._R._Tolkien"
},
{
quote: "Everything around you that you call life was made up by people that were no smarter than you and you can change it, you can influence it, you can build your own things that other people can use.",
author: "Steve Jobs",
profile: "https://www.gannett-cdn.com/-mm-/f4f8cf8c812e00141458a67f4c0a3f8af56ca2d4/c=181-0-1079-898/local/-/media/2015/09/01/USATODAY/USATODAY/635766651669578501-SteveJobs.jpg?width=200&height=200&fit=crop",
link: "https://en.wikipedia.org/wiki/Steve_Jobs",
background: "https://cdn.pixabay.com/photo/2016/07/06/13/21/office-1500461_1280.jpg"
}
];
const colors = [
"#16a085",
"#27ae60",
"#2c3e50",
"#f39c12",
"#e74c3c",
"#9b59b6",
"#FB6964",
"#342224",
"#472E32",
"#BDBB99",
"#77B1A9",
"#73A857"
]
class App extends React.Component {
constructor(props){
super(props);
this.state = {
quote: '',
author: '',
profile: '',
link: '',
currentColor: 0,
}
this.generateQuote = this.generateQuote.bind(this);
}
generateQuote() {
let randomIndex = (Math.floor(Math.random() * Object.keys(quotes).length))
let randomColor = (Math.floor(Math.random() * Object.keys(colors).length))
let quote = quotes[randomIndex].quote;
let author = quotes[randomIndex].author;
let profile = quotes[randomIndex].profile;
let link = quotes[randomIndex].link;
let currentColor = colors[randomColor].currentColor;
this.setState({
quote: quote,
author: author,
profile: profile,
link: link,
currentColor: color,
});
};
componentDidMount(){
this.generateQuote();
};
render(){
return(
<div id="quote-box" style={{ backgroundColor: this.state.colors[this.state.currentColor] }}>
<a href={this.state.link} target="_blank"><img id="profile" src={this.state.profile}/></a>
<Quote quote={this.state.quote} author={this.state.author} link={this.state.link}/>
<Buttons handleNewQuote={this.generateQuote} quote={this.state.quote} author={this.state.author} />
</div>
)
};
};
class Quote extends React.Component{
constructor(props){
super(props)
}
render(){
return(
<div id="wrapper">
<q id="text" className="quote_text">{this.props.quote}</q>
<h3 id="author" className="author_text">{this.props.author}</h3>
</div>
)
}
};
class Buttons extends React.Component{
constructor(props){
super(props);
}
render(){
return(
<div className="buttons">
<button type="button" id="new-quote" onClick={this.props.handleNewQuote}>New Quote
</button>
<a id="tweet-quote" target="_blank" href={`https://twitter.com/intent/tweet/?text=${this.props.quote} - ${this.props.author}`}><button>Tweet This!</button></a>
</div>
)
}
};
ReactDOM.render(
<App />,
document.getElementById('content')
)