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

Warn when Using DefaultProps on Function Components #16210

Merged
merged 8 commits into from Jul 25, 2019
Next

add default props test

  • Loading branch information...
Luna Ruan
Luna Ruan committed Jul 22, 2019
commit 39fee281a922bd415637a14d89ca0a5c042ed325
@@ -206,6 +206,31 @@ describe('ReactFunctionComponent', () => {
ReactTestUtils.renderIntoDocument(<ParentUsingStringRef />);
});

fit('should warn when given defaultProps', () => {
function FunctionalComponent(props) {
return null;
}

FunctionalComponent.defaultProps = {
testProp: true,
};

class Test extends React.Component {
render() {
return null;
}
}

Test.defaultProps = {
testProp: true,
};

expect(() => ReactTestUtils.renderIntoDocument(<Test />)).toWarnDev(
'Warning: defaultProps should only be used on Class Components. ' +
'For Functional Components, use Javascript default arguments instead.',
);
});

it('should warn when given a function ref', () => {
function Indirection(props) {
return <div>{props.children}</div>;
@@ -301,9 +301,12 @@ export function jsxDEV(type, config, maybeKey, source, self) {
* Create and return a new ReactElement of the given type.
* See https://reactjs.org/docs/react-api.html#createelement
*/
// type: component
// config: props + key + ref
export function createElement(type, config, children) {
let propName;

console.log(type, '\n\n', config, '\n\n', children);
console.log('a', type.prototype.isReactComponent());
// Reserved names are extracted
const props = {};

@@ -373,6 +376,8 @@ export function createElement(type, config, children) {
defineRefPropWarningGetter(props, displayName);
}
}
// if (type.defaultProps && type.prototype.isReactComponent) {
// }
}
return ReactElement(
type,
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.