Skip to content

Commit 1bae466

Browse files
Merge pull request #177 from ui-router/remount
Force Component remount when reloading the State
2 parents 256c704 + 4dd0c37 commit 1bae466

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

examples/typescript/components/Child.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import * as React from 'react';
2-
import { UIView } from '@uirouter/react';
2+
import { UIView, UIViewInjectedProps } from '@uirouter/react';
33

4-
export class Child extends React.Component<any, any> {
4+
export class Child extends React.Component<UIViewInjectedProps, any> {
55
uiCanExit = () => {
66
return Promise.resolve();
77
};
8+
componentDidMount() {
9+
console.log('mounted');
10+
}
11+
handleClick = () => {
12+
this.props.transition.router.stateService.reload();
13+
};
814
render() {
915
return (
1016
<div>
1117
<h2>Child</h2>
1218
<UIView />
19+
<button onClick={this.handleClick}>remount</button>
1320
</div>
1421
);
1522
}

src/components/UIView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ class View extends Component<UIViewProps, UIViewState> {
243243
}
244244

245245
this.uiViewData.config = newConfig;
246-
let props = { ...resolves, transition: trans };
246+
const key = Date.now();
247+
let props = { ...resolves, transition: trans, key };
247248

248249
let newComponent = newConfig && newConfig.viewDecl && newConfig.viewDecl.component;
249250
this.setState({

src/components/__tests__/UIView.test.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const states = [
5757

5858
describe('<UIView>', () => {
5959
describe('(unmounted)', () => {
60-
let router;
60+
let router: UIRouterReact;
6161
beforeEach(() => {
6262
router = new UIRouterReact();
6363
router.plugin(memoryLocationPlugin);
@@ -98,12 +98,12 @@ describe('<UIView>', () => {
9898
});
9999

100100
describe('(mounted)', () => {
101-
let router;
101+
let router: UIRouterReact;
102102
beforeEach(() => {
103103
router = new UIRouterReact();
104104
router.plugin(servicesPlugin);
105105
router.plugin(memoryLocationPlugin);
106-
states.forEach(state => router.stateRegistry.register(state));
106+
states.forEach(state => router.stateRegistry.register(state as ReactStateDeclaration));
107107
});
108108

109109
it('renders its State Component', () => {
@@ -123,7 +123,7 @@ describe('<UIView>', () => {
123123
name: '__state',
124124
component: Comp,
125125
resolve: [{ resolveFn: () => true, token: 'myresolve' }],
126-
});
126+
} as ReactStateDeclaration);
127127
const wrapper = mount(
128128
<UIRouter router={router}>
129129
<UIView />
@@ -141,7 +141,7 @@ describe('<UIView>', () => {
141141
name: '__state',
142142
component: Comp,
143143
resolve: [{ token: 'foo', resolveFn: () => 'bar' }],
144-
});
144+
} as ReactStateDeclaration);
145145
const wrapper = mount(
146146
<UIRouter router={router}>
147147
<UIView />
@@ -158,7 +158,7 @@ describe('<UIView>', () => {
158158
name: '__state',
159159
component: Comp,
160160
resolve: [{ token: 'transition', resolveFn: () => null }],
161-
});
161+
} as ReactStateDeclaration);
162162

163163
await router.stateService.go('__state');
164164

@@ -268,5 +268,35 @@ describe('<UIView>', () => {
268268
await router.stateService.go('withrenderprop');
269269
expect(wrapper.html()).toEqual(`<div><span>withrenderprop</span><span>bar</span></div>`);
270270
});
271+
272+
it('unmounts the State Component when calling stateService.reload(true)', async () => {
273+
const componentDidMountWatcher = jest.fn();
274+
const componentWillUnmountWatcher = jest.fn();
275+
class TestUnmountComponent extends React.Component {
276+
componentDidMount() {
277+
componentDidMountWatcher();
278+
}
279+
componentWillUnmount() {
280+
componentWillUnmountWatcher();
281+
}
282+
render() {
283+
return <div />;
284+
}
285+
}
286+
const testState = {
287+
name: 'testunmount',
288+
component: TestUnmountComponent,
289+
};
290+
router.stateRegistry.register(testState);
291+
const wrapper = mount(
292+
<UIRouter router={router}>
293+
<UIView />
294+
</UIRouter>,
295+
);
296+
await router.stateService.go('testunmount');
297+
await router.stateService.reload('testunmount');
298+
expect(componentDidMountWatcher).toHaveBeenCalledTimes(2);
299+
expect(componentWillUnmountWatcher).toHaveBeenCalledTimes(1);
300+
});
271301
});
272302
});

0 commit comments

Comments
 (0)