Skip to content
New issue

Have a question about this project? 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. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UpdateProps now keeps the old props. #6538

Merged
merged 1 commit into from
Sep 7, 2020
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
44 changes: 31 additions & 13 deletions lib/src/components/ComponentWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import hoistNonReactStatics from 'hoist-non-react-statics';
import { Store } from './Store';
import { ComponentEventsObserver } from '../events/ComponentEventsObserver';

interface HocState { componentId: string; allProps: {}; }
interface HocProps { componentId: string; }
interface HocState {
componentId: string;
allProps: {};
}
interface HocProps {
componentId: string;
}

export interface IWrappedComponent extends React.Component {
setProps(newProps: Record<string, any>): void;
Expand All @@ -29,8 +34,8 @@ export class ComponentWrapper {
return {
allProps: {
...nextProps,
...store.getPropsForId(prevState.componentId)
}
...store.getPropsForId(prevState.componentId),
},
};
}

Expand All @@ -39,13 +44,18 @@ export class ComponentWrapper {
this._assertComponentId();
this.state = {
componentId: props.componentId,
allProps: {}
allProps: {},
};
store.setComponentInstance(props.componentId, this);
}

public setProps(newProps: any) {
this.setState({ allProps: newProps });
this.setState((prevState) => ({
allProps: {
...prevState.allProps,
...newProps,
},
}));
}

componentWillUnmount() {
Expand All @@ -55,10 +65,7 @@ export class ComponentWrapper {

render() {
return (
<GeneratedComponentClass
{...this.state.allProps}
componentId={this.state.componentId}
/>
<GeneratedComponentClass {...this.state.allProps} componentId={this.state.componentId} />
);
}

Expand All @@ -70,11 +77,22 @@ export class ComponentWrapper {
}

polyfill(WrappedComponent);
hoistNonReactStatics(WrappedComponent, concreteComponentProvider === OriginalComponentGenerator ? GeneratedComponentClass : concreteComponentProvider());
return ReduxProvider ? this.wrapWithRedux(WrappedComponent, ReduxProvider, reduxStore) : WrappedComponent;
hoistNonReactStatics(
WrappedComponent,
concreteComponentProvider === OriginalComponentGenerator
? GeneratedComponentClass
: concreteComponentProvider()
);
return ReduxProvider
? this.wrapWithRedux(WrappedComponent, ReduxProvider, reduxStore)
: WrappedComponent;
}

wrapWithRedux(WrappedComponent: React.ComponentClass<any>, ReduxProvider: any, reduxStore: any): React.ComponentClass<any> {
wrapWithRedux(
WrappedComponent: React.ComponentClass<any>,
ReduxProvider: any,
reduxStore: any
): React.ComponentClass<any> {
class ReduxWrapper extends React.Component<any, any> {
render() {
return (
Expand Down
13 changes: 10 additions & 3 deletions lib/src/components/Store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ describe('Store', () => {
});

it('clear instance by component id when clear component', () => {
uut.setComponentInstance('refUniqueId', ({} as IWrappedComponent));
uut.setComponentInstance('refUniqueId', {} as IWrappedComponent);
uut.clearComponent('refUniqueId');
expect(uut.getComponentInstance('refUniqueId')).toEqual(undefined);
});

it('holds component instance by id', () => {
uut.setComponentInstance('component1', ({} as IWrappedComponent));
uut.setComponentInstance('component1', {} as IWrappedComponent);
expect(uut.getComponentInstance('component1')).toEqual({});
});

it('calls component setProps when set props by id', () => {
const instance: any = {setProps: jest.fn()};
const instance: any = { setProps: jest.fn() };
const props = { foo: 'bar' };

uut.setComponentInstance('component1', instance);
Expand Down Expand Up @@ -76,4 +76,11 @@ describe('Store', () => {
expect(uut.getComponentClassForName('lazy')).toEqual(MyLazyComponent);
expect(lazyRegistrator).toHaveBeenCalledTimes(1);
});

it('keeps the old props when updateProps is called', () => {
uut.updateProps('component1', { foo: 'foo', bar: 'bar' });
uut.updateProps('component1', { foo: 'foo2' });

expect(uut.getPropsForId('component1')).toEqual({ foo: 'foo2', bar: 'bar' });
});
});
10 changes: 9 additions & 1 deletion lib/src/components/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Store {
private lazyRegistratorFn: ((lazyComponentRequest: string | number) => void) | undefined;

updateProps(componentId: string, props: any) {
this.propsById[componentId] = props;
this.mergeNewPropsForId(componentId, props);
const component = this.componentsInstancesById[componentId];
if (component) {
this.componentsInstancesById[componentId].setProps(props);
Expand All @@ -21,6 +21,14 @@ export class Store {
return this.propsById[componentId] || {};
}

mergeNewPropsForId(componentId: string, newProps: any) {
const currentProps = this.getPropsForId(componentId);
this.propsById[componentId] = {
...currentProps,
...newProps,
};
}

clearComponent(componentId: string) {
delete this.propsById[componentId];
delete this.componentsInstancesById[componentId];
Expand Down