Skip to content

Commit f93f56b

Browse files
committed
Merge branch 'fb-query-string-param'
2 parents 13c0e48 + d23c8c9 commit f93f56b

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

src/components/ErrorDecoder/ErrorDecoder.js

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,48 @@
22
* Copyright (c) 2013-present, Facebook, Inc.
33
*
44
* @emails react-core
5+
* @flow
56
*/
67

78
'use strict';
89

9-
import React, {Component} from 'react';
10-
import PropTypes from 'prop-types';
10+
import React from 'react';
1111

12-
function replaceArgs(msg, argList) {
12+
import type {Node} from 'react';
13+
14+
function replaceArgs(msg: string, argList: Array<string>): string {
1315
let argIdx = 0;
1416
return msg.replace(/%s/g, function() {
1517
const arg = argList[argIdx++];
1618
return arg === undefined ? '[missing argument]' : arg;
1719
});
1820
}
1921

20-
function urlify(str) {
22+
// When the message contains a URL (like https://fb.me/react-refs-must-have-owner),
23+
// make it a clickable link.
24+
function urlify(str: string): Node {
2125
const urlRegex = /(https:\/\/fb\.me\/[a-z\-]+)/g;
2226

2327
const segments = str.split(urlRegex);
2428

25-
for (let i = 0; i < segments.length; i++) {
29+
return segments.map((message, i) => {
2630
if (i % 2 === 1) {
27-
segments[i] = (
28-
<a key={i} target="_blank" rel="noopener" href={segments[i]}>
29-
{segments[i]}
31+
return (
32+
<a key={i} target="_blank" rel="noopener" href={message}>
33+
{message}
3034
</a>
3135
);
3236
}
33-
}
34-
35-
return segments;
37+
return message;
38+
});
3639
}
3740

38-
// ?invariant=123&args[]=foo&args[]=bar
39-
function parseQueryString(location) {
40-
const rawQueryString = location.search.substring(1);
41+
// `?invariant=123&args[]=foo&args[]=bar`
42+
// or `// ?invariant=123&args[0]=foo&args[1]=bar`
43+
function parseQueryString(
44+
search: string,
45+
): ?{|code: string, args: Array<string>|} {
46+
const rawQueryString = search.substring(1);
4147
if (!rawQueryString) {
4248
return null;
4349
}
@@ -50,15 +56,15 @@ function parseQueryString(location) {
5056
const query = decodeURIComponent(queries[i]);
5157
if (query.indexOf('invariant=') === 0) {
5258
code = query.slice(10);
53-
} else if (query.indexOf('args[]=') === 0) {
54-
args.push(query.slice(7));
59+
} else if (query.indexOf('args[') === 0) {
60+
args.push(query.slice(query.indexOf(']=') + 2));
5561
}
5662
}
5763

58-
return [code, args];
64+
return {args, code};
5965
}
6066

61-
function ErrorResult(props) {
67+
function ErrorResult(props: {|code: ?string, msg: string|}) {
6268
const code = props.code;
6369
const errorMsg = props.msg;
6470

@@ -79,39 +85,21 @@ function ErrorResult(props) {
7985
);
8086
}
8187

82-
class ErrorDecoder extends Component {
83-
constructor(...args) {
84-
super(...args);
85-
86-
this.state = {
87-
code: null,
88-
errorMsg: '',
89-
};
88+
function ErrorDecoder(props: {|
89+
errorCodesString: string,
90+
location: {search: string},
91+
|}) {
92+
let code = null;
93+
let msg = '';
94+
95+
const errorCodes = JSON.parse(props.errorCodesString);
96+
const parseResult = parseQueryString(props.location.search);
97+
if (parseResult != null) {
98+
code = parseResult.code;
99+
msg = replaceArgs(errorCodes[code], parseResult.args);
90100
}
91101

92-
componentWillMount() {
93-
const {errorCodesString} = this.props;
94-
const errorCodes = JSON.parse(errorCodesString);
95-
const parseResult = parseQueryString(this.props.location);
96-
if (parseResult != null) {
97-
const [code, args] = parseResult;
98-
if (errorCodes[code]) {
99-
this.setState({
100-
code: code,
101-
errorMsg: replaceArgs(errorCodes[code], args),
102-
});
103-
}
104-
}
105-
}
106-
107-
render() {
108-
return <ErrorResult code={this.state.code} msg={this.state.errorMsg} />;
109-
}
102+
return <ErrorResult code={code} msg={msg} />;
110103
}
111104

112-
ErrorDecoder.propTypes = {
113-
errorCodesString: PropTypes.string.isRequired,
114-
location: PropTypes.object.isRequired,
115-
};
116-
117105
export default ErrorDecoder;

0 commit comments

Comments
 (0)