From 986d97788c5a8a0eb063c2215b86d22457a04443 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 20 Nov 2017 22:05:00 +0000 Subject: [PATCH] Minor FAQ updates (#308) * Don't use experimental syntax unless necessary * Edit another page * Update another page * Unrelated bikeshed on virtual DOM * Please, no *.jsx, we don't recommend it * Update faq-structure.md * Minor updates * Update faq-ajax.md * Update faq-functions.md * Add binding explanation --- content/docs/faq-ajax.md | 42 ++++++----- content/docs/faq-functions.md | 131 +++++++++++++++++++++++----------- content/docs/faq-internals.md | 6 +- content/docs/faq-state.md | 17 ++--- content/docs/faq-structure.md | 16 ++--- content/docs/faq-styling.md | 16 ++++- 6 files changed, 152 insertions(+), 76 deletions(-) diff --git a/content/docs/faq-ajax.md b/content/docs/faq-ajax.md index cd4cc330..420ac6b7 100644 --- a/content/docs/faq-ajax.md +++ b/content/docs/faq-ajax.md @@ -31,27 +31,35 @@ The example API returns a JSON object like this: ```jsx class MyComponent extends React.Component { - state = { - error: null, - isLoaded: false, - items: [] - }; + constructor(props) { + super(props); + this.state = { + error: null, + isLoaded: false, + items: [] + }; + } componentDidMount() { fetch("https://api.example.com/items") .then(res => res.json()) - .then(result => - this.setState({ - isLoaded: true, - items: result.items - }) + .then( + (result) => { + this.setState({ + isLoaded: true, + items: result.items + }); + }, + // Note: it's important to handle errors here + // instead of a catch() block so that we don't swallow + // exceptions from actual bugs in components. + (error) => { + this.setState({ + isLoaded: true, + error + }); + } ) - .catch(error => - this.setState({ - isLoaded: true, - error - }) - ); } render() { @@ -59,7 +67,7 @@ class MyComponent extends React.Component { if (error) { return
Error: {error.message}
; } else if (!isLoaded) { - return
Loading ...
; + return
Loading...
; } else { return (