-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tahuana's assignment #6
base: master
Are you sure you want to change the base?
Changes from 1 commit
6bde0d6
0b25393
f781dbb
80c8b28
e577d59
f8638ec
5c48f0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,30 +3,37 @@ import logo from './logo.svg'; | |
import './App.css'; | ||
import { Credentials } from './config/Config.js' | ||
import Recipe from './components/Recipe.js' | ||
import Error from './components/Error.js' | ||
|
||
class App extends Component { | ||
constructor(){ | ||
super(); | ||
this.state = { | ||
recipeList: [] | ||
recipeList: [], | ||
errorMessage: [] | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
const q = 'cakes' | ||
const endpoint = `${Credentials.URL}?app_id=${Credentials.APP_ID}&app_key=${Credentials.APP_KEY}&q=${q}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
//const endpoint = `${Credentials.URL}?app_id=${Credentials.APP_ID}&app_key=${Credentials.APP_KEY}` | ||
|
||
const fetchRecipes = () => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider pulling this function outside of the scope of Something like this
|
||
fetch(endpoint) | ||
.then(response => response.json()) | ||
.then(response => this.setState({recipeList: response.hits})) | ||
.catch( (error) => console.log(error)) | ||
.catch( (error) => { | ||
console.log("error: ", error) | ||
this.setState({errorMessage: [error.message]}) | ||
}) | ||
|
||
fetchRecipes(); | ||
|
||
} | ||
|
||
render() { | ||
|
||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
|
@@ -35,11 +42,14 @@ class App extends Component { | |
</header> | ||
<div className="recipe-list"> | ||
<p>Recipe List:</p> | ||
{this.state.recipeList.map(recipe => <Recipe myRecipe = {recipe} />)} | ||
{this.state.recipeList.map(recipe => <Recipe myRecipe = {recipe} />)} | ||
{this.state.errorMessage.map(error => <Error message={error} />)} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default App; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React, { Component } from 'react'; | ||
|
||
class Error extends Component { | ||
constructor(props){ | ||
super(props) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with this constructor! You don't need this :) |
||
|
||
render() { | ||
console.log("estou no erro") | ||
return ( | ||
<div> | ||
<p>Ops... we got an error: {this.props.message}</p> | ||
</div> | ||
) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since you're not manipulating state or using any of the lifecycle hooks, you can make this a stateless component. Rather than extending the export const Error = (props) =>
<div>
{
console.log("estou no erro")
}
<p>Ops... we got an error: {props.message}</p>
</div>
export default Error; You can try doing the same with your recipe component as well! |
||
|
||
|
||
export default Error; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you've got the concepts of a HOC down! Nice work 😄 - this is a really complicated concept!
One thing you could do to make this more generic is pass your props directly to your pass and fail components instead of manipulating the props here, similar to this:
Then you can use the data however you'd like within the component itself, which means you could pass any components to your
branch
component and reuse yourbranch
component multiple times.One other thing to double check is that you're passing your parameters into your HOC in the order you expect them - when you call this on line 67 you're passing 1) your condition, 2) your success component (Recipe) and 3) your fail component (Error). Here, you're rendering your
Error
component as the success component (which I think is is what you actually want to render, since you're checking forhasError
). It could be worth renaming yourtest
boolean something likepassCondition
or similar so it's clear that the first returned component is the one you want rendered if your condition is true. And if you want yourRecipe
component to show with the success of your condition, you could change your boolean to!this.state.hasError
.Excellent work with Higher Order Components!