I am trying to run a react app on codepen. It is the Random Quote Generator project of Front end libraries. I checked the syntax and everything seems to be fine. Even so, there is no output. Here is the link to my project - https://codepen.io/ambarish-gokhale/pen/LYpxLzy
Please help me find what is wrong with my project!
This does not make much sense, as you are not calling this on componentDidMount anywhere. It is a built-in method. There is no need to bind this to it.
You appear to have fixed the syntax error.
return(
<div> {/* You need this div as a wrapper */}
<h1 className="title">Random Quote Generator</h1>
<Box quote={this.quote} author={this.author} />
<TwitterShare quote={this.quote} author={this.author} />
<Button id="new-quote" title="New Quote" onClick={this.getNewQuote} />
</div>
);
When you write a component in React, you will, hopefully, attach it to an element: Scenario 1
const Comp = () => (<div><p>Hello World</p><p>Hi World</p></div>)
...
// Inside your main component:
return(
<div>
<Comp />
</div>
);
Because the Comp component has 1 parent element inside it, attaching it to another element is simple; You attach 1 thing in its place.
Scenario 2
// Notice, this component has 2 elements on the same level
const Comp = () => (<p>Hello World</p><p>Hi World</p>)
...
// Inside your main component:
return(
<div>
<Comp /> {/* You are attaching 1 element */}
</div>
);
Now, imagine you are a computer. You have 1 space reserved for a component’s elements. However, that component is trying to append 2 elements to your 1 space. How do you arrange this? Who goes where?
Please do realise that the above is over-dramatised. I suggest you research this for yourself. Perhaps, watch this video: Deconstructing React
Change functions inside QuoteBox component into methods.
Fix all of your calls to this.quote and this.author.
Pass your methods into the functional components as props.
Fix all calls to methods using this.
A bunch more…
Look, you have many repeated fundamental issues with your code. All I can suggest is for you to take another shot at the React part of the fCC course.
You need to focus on:
Difference between a functional component, and a class component.
How to pass state as props.
How to call props.
How to pass methods as props.
How to set state in functional and class components.
How to reference state within a component.
Basic classes - What is a method.
Number 7 above is quite important, and should probably come first. The fCC curriculum does not cover this very well, as it is just brushed over. I suggest you do your own research into this.
I am sorry I could not be of more help, but I will not just give you the code.
You’ve helped me more than enough. I’m a slow learner anyway. I’ll re-visit those chapters you mentioned. Thank you for everything!
Update: I completed the project!