Skip to content

Commit be273c0

Browse files
Merge pull request cerebral#122 from cerebral/reactWrapperFix
fix(overmind-react): allow connected components to operate as normal
2 parents d2132b4 + 2231e4b commit be273c0

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

packages/node_modules/overmind-react/src/__snapshots__/index.test.tsx.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`React should allow using component as normal, event when connected 1`] = `
4+
<h1>
5+
bar
6+
</h1>
7+
`;
8+
9+
exports[`React should allow using component as normal, event when connected 2`] = `
10+
<h1>
11+
nada
12+
</h1>
13+
`;
14+
315
exports[`React should connect actions and state to class components 1`] = `
416
<h1>
517
bar

packages/node_modules/overmind-react/src/index.test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,39 @@ describe('React', () => {
9696
expect(tree).toMatchSnapshot()
9797
})
9898

99+
test('should allow using component as normal, event when connected', () => {
100+
expect.assertions(2)
101+
const config = {
102+
state: {
103+
foo: 'bar',
104+
},
105+
}
106+
107+
type IApp = TApp<{
108+
state: {
109+
foo: typeof config.state.foo
110+
}
111+
}>
112+
113+
const app = new Overmind(config)
114+
115+
const connect = createConnect(app)
116+
117+
class Component extends React.Component<TConnect<typeof app>> {
118+
render() {
119+
const { app } = this.props
120+
121+
return <h1>{app ? app.state.foo : 'nada'}</h1>
122+
}
123+
}
124+
const ConnectedComponent = connect(Component)
125+
const tree = renderer.create(<ConnectedComponent />).toJSON()
126+
const tree2 = renderer.create(<Component app={null} />).toJSON()
127+
128+
expect(tree).toMatchSnapshot()
129+
expect(tree2).toMatchSnapshot()
130+
})
131+
99132
test('should connect reactions to components', () => {
100133
expect.assertions(2)
101134
let reactionCount = 0

packages/node_modules/overmind-react/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export const createConnect = <App extends Overmind<any>>(app: App) => {
4040
const originalWillUnmount = Component.prototype.componentWillUnmount
4141

4242
Component.prototype.componentWillUnmount = function() {
43+
if (!this.props.app) {
44+
return originalWillUnmount && originalWillUnmount.call(this)
45+
}
46+
4347
this.__isUnmounting = true
4448
if (this.__mutationListener) {
4549
app.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
@@ -53,6 +57,10 @@ export const createConnect = <App extends Overmind<any>>(app: App) => {
5357
originalWillUnmount && originalWillUnmount.call(this)
5458
}
5559
Component.prototype.render = function() {
60+
if (!this.props.app) {
61+
return originalRender.call(this)
62+
}
63+
5664
if (typeof this.__componentId === 'undefined') {
5765
this.__componentId = nextComponentId++
5866
}

0 commit comments

Comments
 (0)