Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
initialize state to null if not when not set (#1310)
Browse files Browse the repository at this point in the history
* initialize state to null if not set by components constructor, fixes #1249

* add changelog
  • Loading branch information
Matt authored and James Baxley committed Oct 31, 2017
1 parent 66f30a6 commit b52b017
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

### vNext
- BREAKING: removed cleanupApolloState as it is no longer needed!
- Exported getDataFromTree on the client
- Exported getDataFromTree on the client
- Removed `redux` from peer dependencies. [Issue #1223](https://github.com/apollographql/react-apollo/issues/1223) [PR #1224](https://github.com/apollographql/react-apollo/pull/1224)
- Support arrays being returned from render in SSR [#1158](https://github.com/apollographql/react-apollo/pull/1158)
- Support passing an updater function to `setState` in SSR mode [#1263](https://github.com/apollographql/react-apollo/pull/1263)
- Correctly initializes component state as null (not undefined) [#1300](https://github.com/apollographql/react-apollo/pull/1310)

### 2.0.0-beta.0
- upgrade to Apollo Client 2.0
Expand Down
2 changes: 2 additions & 0 deletions src/getDataFromTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export function walkTree<Cache>(
// In case the user doesn't pass these to super in the constructor
instance.props = instance.props || props;
instance.context = instance.context || context;
// set the instance state to null (not undefined) if not set, to match React behaviour
instance.state = instance.state || null;

// Override setState to just change the state, not queue up an update.
// (we can't do the default React thing as we aren't mounted "properly"
Expand Down
26 changes: 26 additions & 0 deletions test/react-web/server/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,32 @@ describe('SSR', () => {
.catch(console.error);
});

it('should correctly initialize an empty state to null', () => {
class Element extends React.Component<any, any> {
render() {
expect(this.state).toBeNull();
}
}

return getDataFromTree(<Element />);
});

it('should maintain any state set in the element constructor', () => {
class Element extends React.Component<any, any> {
s;
constructor(props) {
super(props);
this.state = { foo: 'bar' };
}

render() {
expect(this.state).toEqual({ foo: 'bar' });
}
}

return getDataFromTree(<Element />);
});

it('should allow for setting state via an updater function', done => {
const query = gql`
query user($id: ID) {
Expand Down

0 comments on commit b52b017

Please sign in to comment.