Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix(web): 修复页面组件无法获取 props.location.query 的问题 (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck authored Jul 3, 2020
1 parent af8db50 commit 7422aff
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion e2e/dummy/src/app.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pages = ['pages/one/index', 'pages/two/index'];
const pages = ['pages/one/index', 'pages/two/index', 'pages/query/index'];

module.exports.ali = {
pages,
Expand Down
14 changes: 14 additions & 0 deletions e2e/dummy/src/pages/query/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as React from 'react';
import { useQuery } from 'remax';
import { View } from 'remax/one';

export default props => {
const query = useQuery();

return (
<View>
<View>query from props: {props.location.query.name}</View>
<View>query from hook: {query.name}</View>
</View>
);
};
9 changes: 1 addition & 8 deletions e2e/dummy/src/pages/two/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import * as React from 'react';
import { useQuery } from 'remax';
import { View } from 'remax/one';

export default () => {
const query = useQuery();
return (
<View>
page two
<View>{query.q}</View>
</View>
);
return <View>page two</View>;
};
7 changes: 4 additions & 3 deletions e2e/dummy/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ describe('works', () => {
await expect(page).toMatch('page two');
});

it('useQuery', async () => {
await goTo('/pages/two/index?q=query');
await expect(page).toMatch('query');
it('passes query to page', async () => {
await goTo('/pages/query/index?name=foo');
await expect(page).toMatch('query from props: foo');
await expect(page).toMatch('query from hook: foo');
});
});
12 changes: 6 additions & 6 deletions packages/remax-runtime/src/__tests__/createPageWrapper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('createPageWrapper', () => {
}

it('get Page ref', () => {
const WrappedPage = createPageWrapper(Page, {});
const testRenderer = TestRenderer.create(<WrappedPage page={null} />);
const WrappedPage = createPageWrapper(Page);
const testRenderer = TestRenderer.create(<WrappedPage page={null} query={{}} />);
const instance = testRenderer.getInstance() as any;
expect(instance.pageComponentInstance).toBeInstanceOf(Page);
});
Expand All @@ -22,16 +22,16 @@ describe('createPageWrapper', () => {
return <PageComponent ref={ref} />;
});
};
const WrappedPage = createPageWrapper(hoc(Page), {});
const testRenderer = TestRenderer.create(<WrappedPage page={null} />);
const WrappedPage = createPageWrapper(hoc(Page));
const testRenderer = TestRenderer.create(<WrappedPage page={null} query={{}} />);
const instance = testRenderer.getInstance() as any;
expect(instance.pageComponentInstance).toBeInstanceOf(Page);
});

it('does not pass ref to FC', () => {
const FCPage: React.FC = props => <view>hi</view>;
const WrappedPage = createPageWrapper(FCPage, {});
const testRenderer = TestRenderer.create(<WrappedPage page={null} />);
const WrappedPage = createPageWrapper(FCPage);
const testRenderer = TestRenderer.create(<WrappedPage page={null} query={{}} />);
const instance = testRenderer.getInstance() as any;
expect(instance.pageComponentInstance).toBeNull();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/remax-runtime/src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,9 @@ it('usePageInstance works', done => {
}, []);

return <View />;
}, {});
});
const container = new Container(p);
render(<Page page={{ data: {} }} />, container);
render(<Page page={{ data: {} }} query={{}} />, container);
});

describe('Remax Suspense placeholder', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/remax-runtime/src/createPageConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ export default function createPageConfig(Page: React.ComponentType<any>, name: s
lifecycleCallback: {} as any,

onLoad(this: any, query: any) {
const PageWrapper = createPageWrapper(Page, query);
const PageWrapper = createPageWrapper(Page);
this.pageId = generatePageId();

this.query = query;
this.container = new Container(this);
this.element = createPortal(
React.createElement(PageWrapper, {
page: this,
query,
ref: this.wrapperRef,
}),
this.container,
Expand Down
10 changes: 7 additions & 3 deletions packages/remax-runtime/src/createPageConfig.web.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { PullToRefresh } from '@remax/web';
import qs from 'qs';
import createPageWrapper from './createPageWrapper';
import { Lifecycle, callbackName } from './lifecycle';

Expand All @@ -10,13 +11,13 @@ interface LifeCycleCallback {
interface PageConfigProps {
appConfig: any;
pageConfig: any;
location: any;
}

const DEFAULT_REACH_BOTTOM_DISTANCE = 50;

export default function createPageConfig(Page: React.ComponentType<any>) {
const page = {
query: {},
lifeCycleCallback: {} as LifeCycleCallback,
wrapperRef: React.createRef<any>(),
registerLifecycle(lifeCycle: Lifecycle, callback: () => any) {
Expand Down Expand Up @@ -45,7 +46,7 @@ export default function createPageConfig(Page: React.ComponentType<any>) {
},
};

const PageWrapper = createPageWrapper(Page, page.query);
const PageWrapper = createPageWrapper(Page);

return class PageConfig extends React.Component<PageConfigProps> {
constructor(props: any) {
Expand Down Expand Up @@ -145,17 +146,19 @@ export default function createPageConfig(Page: React.ComponentType<any>) {
};

render() {
const { appConfig } = this.props;
const { appConfig, location } = this.props;
const { refreshing } = this.state;
const hasTabBar = !!appConfig.tabBar;
const className = `remax-page ${hasTabBar ? 'with-tab-bar' : ''}`;
const query = qs.parse(location.search, { ignoreQueryPrefix: true });

if (this.isPullDownRefreshEnabled()) {
return (
<PullToRefresh onRefresh={this.handleRefresh} refreshing={refreshing} distanceToRefresh={50}>
<div className={className}>
{React.createElement(PageWrapper, {
page,
query,
ref: page.wrapperRef,
})}
</div>
Expand All @@ -167,6 +170,7 @@ export default function createPageConfig(Page: React.ComponentType<any>) {
<div className={className}>
{React.createElement(PageWrapper, {
page,
query,
ref: page.wrapperRef,
})}
</div>
Expand Down
6 changes: 3 additions & 3 deletions packages/remax-runtime/src/createPageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export interface PageProps<Q = {}> {
};
}

export default function createPageWrapper(Page: React.ComponentType<any>, query: object) {
return class PageWrapper extends React.Component<{ page: any }> {
export default function createPageWrapper(Page: React.ComponentType<any>) {
return class PageWrapper extends React.Component<{ page: any; query: any }> {
// 页面组件的实例
pageComponentInstance: any = null;

Expand Down Expand Up @@ -43,7 +43,7 @@ export default function createPageWrapper(Page: React.ComponentType<any>, query:
render() {
const props: any = {
location: {
query: query || {},
query: this.props.query || {},
},
};

Expand Down

1 comment on commit 7422aff

@vercel
Copy link

@vercel vercel bot commented on 7422aff Jul 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.