<div id="root"></div>
.error {
width: 800px;
border: 1px solid #e01849;
border-radius: 2px;
background: #f8114ae6;
color: #fff;
padding: 25px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
margin: 25px;
}
.ability {
width: 800px;
border: 1px solid #eee;
border-radius: 5px;
background: #5e9af9;
color: #fff;
text-transform: uppercase;
padding: 25px;
height: 100px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 25px;
}
.ability__name {
font-size: 20px;
font-weight: bold;
}
.app {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji',' Segoe UI Emoji', 'Segoe UI Symbol';
display: flex;
flex-direction: column;
align-items: center;
background-color: #f7fbff;
min-height: 100vh;
justify-content: center;
background: url("../background.png");
}
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = {
hasError: false
};
}
componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true });
// You can also log the error to an error reporting service
// Just logging it in the console for demo purposes
console.error(error, info);
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return (
<div className="error">
<div className="error__title">
Something went wrong! Please refresh the page.
</div>
</div>
);
}
return this.props.children;
}
}
const Ability = ({ability}) => {
return (
<div className="ability">
<div className="ability__name">
{ ability.props.ability.name }
</div>
</div>
);
};
class App extends React.Component {
constructor() {
super();
this.state = {
data: []
};
}
componentWillMount() {
fetch("https://pokeapi.co/api/v2/pokemon/1/")
.then(data => data.json())
.then(data => {
this.setState({
data: data.abilities
});
});
}
errorBoundaryNode = (ability, index) => {
return (
<ErrorBoundary key={index}>
<Ability ability={ability} />
</ErrorBoundary>
);
}
abilitiesNode = () => {
return this.state.data.map((ability, index) => {
return this.errorBoundaryNode(ability, index);
});
}
render() {
return (
<div className="app">
{ this.abilitiesNode() }
</div>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
View Compiled
This Pen doesn't use any external CSS resources.