This file contains 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
class Quiz extends Component { | |
componentWillMount() { | |
axios.get('/thedata').then(res => { | |
this.setState({items: res.data}); | |
}); | |
} | |
render() { | |
return ( | |
<ul> |
This file contains 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
class Quiz extends Component { | |
// Added this: | |
constructor(props) { | |
super(props); | |
// Assign state itself, and a default value for items | |
this.state = { | |
items: [] | |
}; | |
} |
This file contains 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
render() { | |
return ( | |
<ul> | |
{this.state && this.state.items && this.state.items.map(item => | |
<li key={item.id}>{item.name}</li> | |
)} | |
</ul> | |
); | |
} |
This file contains 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
class Quiz extends React.Component { | |
constructor(props) { | |
super(props); | |
// Initialized, but not enough | |
this.state = {}; | |
} | |
componentWillMount() { | |
// Get the data "soon" |
This file contains 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
function ItemList({ items }) { | |
return ( | |
// same as above | |
); | |
} | |
ItemList.propTypes = { | |
items: React.PropTypes.array.isRequired | |
}; |
This file contains 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
class MyComponent extends React.Component { | |
render() { | |
// ... | |
} | |
} | |
MyComponent.defaultProps = { | |
items: [] | |
}; |
This file contains 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
class MyComponent extends React.Component { | |
static defaultProps = { | |
items: [] | |
} | |
render() { | |
// ... | |
} | |
} |
This file contains 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
class MyComponent extends React.Component { | |
render() { | |
const { items = [] } = this.props; | |
return ( | |
<ItemList items={items}/> | |
); | |
} | |
} |
This file contains 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 { items = [] } = this.props; |
This file contains 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
function ItemList({ items = []}) { | |
return ( | |
// Use items here. It'll default to an empty array. | |
); | |
} |
OlderNewer