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

escape hatch for context changes #162

Merged
merged 2 commits into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Expect active development and potentially significant breaking changes in the `0

### vNext

- Bug: Fixed issue when context changes in parent container not going through to child; [#162](https://github.com/apollostack/react-apollo/pull/162)
- Bug: Fixed loading state on remount of forceFetch operations; [#161](https://github.com/apollostack/react-apollo/pull/161)

### v0.4.6
Expand Down
4 changes: 2 additions & 2 deletions src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ export default function graphql(
this.subscribeToQuery(nextProps);
}

shouldComponentUpdate(nextProps, nextState) {
return this.haveOwnPropsChanged || this.hasOperationDataChanged;
shouldComponentUpdate(nextProps, nextState, nextContext) {
return !!nextContext || this.haveOwnPropsChanged || this.hasOperationDataChanged;
}

componentWillUnmount() {
Expand Down
72 changes: 72 additions & 0 deletions test/react-web/client/graphql/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -876,4 +876,76 @@ describe('queries', () => {
mount(<ProviderMock client={client}><Container /></ProviderMock>);
});

it('allows context through updates', (done) => {
const query = gql`query people { allPeople(first: 1) { people { name } } }`;
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
const networkInterface = mockNetworkInterface({ request: { query }, result: { data } });
const client = new ApolloClient({ networkInterface });

@graphql(query)
class Container extends React.Component<any, any> {
componentWillReceiveProps(props) {
expect(props.data.loading).to.be.false;
expect(props.data.allPeople).to.deep.equal(data.allPeople);
}
render() {
return <div>{this.props.children}</div>;
}
};

class ContextContainer extends React.Component<any, any> {

constructor(props) {
super(props);
this.state = { color: 'purple' };
}

getChildContext() {
return { color: this.state.color };
}

componentDidMount() {
setTimeout(() => {
this.setState({ color: 'green' });
}, 50);
}

render() {
return <div>{this.props.children}</div>;
}
}

(ContextContainer as any).childContextTypes = {
color: React.PropTypes.string,
};

let count = 0;
class ChildContextContainer extends React.Component<any, any> {
render() {
const { color } = (this.context as any);
if (count === 0) expect(color).to.eq('purple');
if (count === 1) {
expect(color).to.eq('green');
done();
}

count++;
return <div>{this.props.children}</div>;
}
}

(ChildContextContainer as any).contextTypes = {
color: React.PropTypes.string,
};

mount(
<ProviderMock client={client}>
<ContextContainer>
<Container>
<ChildContextContainer />
</Container>
</ContextContainer>
</ProviderMock>);
});

});