Promise-returning setState and forceUpdate by jamiebuilds · Pull Request #10 · reactjs/rfcs · GitHub

  • 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.
  • Already on GitHub?
  • Sign in to your account

[Rendered]
This RFC specifies setState and forceUpdate to return a promise when no callback is provided.
class DataFetcher extends React.Component {
state = {
loading: false,
error: null,
data: null,
};

async componentDidMount() {
await this.setState({ loading: true });
try {
let response = await fetch(this.props.url);
let data = await response.json();
await this.setState({ data, loading: false });
} catch (error) {
await this.setState({ error, loading: false });
}
};

render() {
return this.props.render(this.state);
}
}

Note: This example doesn’t rely so much on setState being applied before continuing. I can come up with a better example if people want it better demonstrated. I figured the data fetching example was easiest to understand.

GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.

class DataFetcher extends React.Component { state = { loading: false, error: null, data: null, }; async componentDidMount() { await this.setState({ loading: true }); try { let response = await fetch(this.props.url); let data = await response.json(); await this.setState({ data, loading: false }); } catch (error) { await this.setState({ error, loading: false }); } }; render() { return this.props.render(this.state); } }

being applied before continuing. I can come up with a better example if people want it better demonstrated. I figured the data fetching example was easiest to understand.

“Yes, we’d have to tag it as a node to visit when committing changes. setState() without callback doesn’t need that.” – Dan

“Yes, we’d have to tag it as a node to visit when committing changes. setState() without callback doesn’t need that.” – Dan

Depending on how big of an impact this makes, I would consider this a big enough reason to reject this proposal outright.

Depending on how big of an impact this makes, I would consider this a big enough reason to reject this proposal outright.

to know whether to return a Promise create a Promise.

to know whether to return a Promise create a Promise.

instance or not.

instance or not.

I think the bigger concern is more:

Promise-returning setState and forceUpdate by jamiebuilds · Pull Request #10 · reactjs/rfcs · GitHub