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

Convert React.createClass to React.Component #877

Merged
merged 1 commit into from
Apr 10, 2017
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
10 changes: 7 additions & 3 deletions docs/api/ReactWrapper/matchesElement.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ render tree.


```jsx
const MyComponent = React.createClass({
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
...
},
}
render() {
return (
<div onClick={this.handleClick} className="foo bar">Hello</div>
);
}
});
}

const wrapper = mount(<MyComponent />);
expect(wrapper.matchesElement(
Expand Down
12 changes: 6 additions & 6 deletions docs/api/ReactWrapper/setContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ NOTE: can only be called on a wrapper instance that is also the root instance.
#### Example

```jsx
const SimpleComponent = React.createClass({
contextTypes: {
name: React.PropTypes.string,
},
class SimpleComponent extends React.Component {
render() {
return <div>{this.context.name}</div>;
},
});
}
}
SimpleComponent.contextTypes = {
name: React.PropTypes.string,
};
```
```jsx
const context = { name: 'foo' };
Expand Down
10 changes: 7 additions & 3 deletions docs/api/ShallowWrapper/matchesElement.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ render tree.


```jsx
const MyComponent = React.createClass({
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
...
},
}
render() {
return (
<div onClick={this.handleClick} className="foo bar">Hello</div>
);
}
});
}

const wrapper = shallow(<MyComponent />);
expect(wrapper.matchesElement(
Expand Down
4 changes: 2 additions & 2 deletions docs/api/ShallowWrapper/prop.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ of the root node of the component.


```jsx
const MyComponent = React.createClass({
class MyComponent extends React.Component {
render() {
return (
<div className="foo bar" includedProp={this.props.includedProp}>Hello</div>
)
}
})
}
const wrapper = shallow(<MyComponent includedProp="Success!" excludedProp="I'm not included" />);
expect(wrapper.prop('includedProp')).to.equal("Success!");

Expand Down
4 changes: 2 additions & 2 deletions docs/api/ShallowWrapper/props.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ See [`.instance() => ReactComponent`](instance.md)


```jsx
const MyComponent = React.createClass({
class MyComponent extends React.Component {
render() {
return (
<div className="foo bar" includedProp={this.props.includedProp}>Hello</div>
)
}
})
}
const wrapper = shallow(<MyComponent includedProp="Success!" excludedProp="I'm not included" />);
expect(wrapper.props().includedProp).to.equal("Success!");

Expand Down
12 changes: 6 additions & 6 deletions docs/api/ShallowWrapper/setContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ NOTE: can only be called on a wrapper instance that is also the root instance.
#### Example

```jsx
const SimpleComponent = React.createClass({
contextTypes: {
name: React.PropTypes.string,
},
class SimpleComponent extends React.Component {
render() {
return <div>{this.context.name}</div>;
},
});
}
}
SimpleComponent.contextTypes = {
name: React.PropTypes.string,
};
```
```jsx
const context = { name: 'foo' };
Expand Down
12 changes: 6 additions & 6 deletions docs/api/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ describe('<Foo />', () => {
});

it('can pass in context', () => {
const SimpleComponent = React.createClass({
contextTypes: {
name: React.PropTypes.string,
},
class SimpleComponent extends React.Component {
render() {
return <div>{this.context.name}</div>;
},
});
}
}
SimpleComponent.contextTypes = {
name: React.PropTypes.string,
};

const context = { name: 'foo' };
const wrapper = render(<SimpleComponent />, { context });
Expand Down
62 changes: 25 additions & 37 deletions src/ReactWrapperComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,54 @@ import objectAssign from 'object.assign';
* pass new props in.
*/
export default function createWrapperComponent(node, options = {}) {
const spec = {

propTypes: {
Component: PropTypes.oneOfType([PropTypes.func, PropTypes.string]).isRequired,
props: PropTypes.object.isRequired,
context: PropTypes.object,
},

getDefaultProps() {
return {
context: null,
};
},

getInitialState() {
return {
class WrapperComponent extends React.Component {
Copy link
Member

Choose a reason for hiding this comment

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

we definitely shouldn't change the specs here; we should continue to use createClass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ljharb What's the reason to continue to use createClass? It seems to be able to replace with ES2015 classes. Is there any problems?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would be nice if Enzyme users don't have to install create-react-class to use it.

Copy link
Member

Choose a reason for hiding this comment

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

oops, you're right, this one isn't a spec file - this can stay as-is :-)

constructor(...args) {
super(...args);
this.state = {
mount: true,
props: this.props.props,
context: this.props.context,
};
},

}
setChildProps(newProps, callback = undefined) {
const props = objectAssign({}, this.state.props, newProps);
this.setState({ props }, callback);
},

setChildContext(context) {
return new Promise(resolve => this.setState({ context }, resolve));
},

}
getInstance() {
const component = this._reactInternalInstance._renderedComponent;
const inst = component.getPublicInstance();
if (inst === null) {
return component._instance;
}
return inst;
},

}
getWrappedComponent() {
const component = this._reactInternalInstance._renderedComponent;
const inst = component.getPublicInstance();
if (inst === null) {
return component._instance;
}
return inst;
},

}
setChildContext(context) {
return new Promise(resolve => this.setState({ context }, resolve));
}
render() {
const { Component } = this.props;
const { mount, props } = this.state;
if (!mount) return null;
return (
<Component {...props} />
);
},
}
}
WrapperComponent.propTypes = {
Component: PropTypes.oneOfType([PropTypes.func, PropTypes.string]).isRequired,
props: PropTypes.object.isRequired,
context: PropTypes.object,
};
WrapperComponent.defaultProps = {
context: null,
};

if (options.context && (node.type.contextTypes || options.childContextTypes)) {
Expand All @@ -81,13 +72,10 @@ export default function createWrapperComponent(node, options = {}) {
if (options.childContextTypes) {
objectAssign(childContextTypes, options.childContextTypes);
}
objectAssign(spec, {
childContextTypes,
getChildContext() {
return this.state.context;
},
});
WrapperComponent.prototype.getChildContext = function getChildContext() {
return this.state.context;
};
WrapperComponent.childContextTypes = childContextTypes;
}

return React.createClass(spec);
return WrapperComponent;
}