Skip to content

Commit

Permalink
Merge pull request #14 from bbxjs/fix/connect
Browse files Browse the repository at this point in the history
fixed connect
  • Loading branch information
zinkey committed Aug 21, 2018
2 parents 50ffa9b + 70e54d1 commit aeb1df4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
7 changes: 1 addition & 6 deletions examples/func-test/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ class CountState extends State {
const countState = new CountState();


@connect(countState)
class Preview extends React.Component {
render() {
return <h1>{countState.state.count}</h1>;
}
}
const Preview = connect(countState)(() => <h1>{countState.state.count}</h1>);

@connect(countState)
class App extends React.Component {
Expand Down
27 changes: 16 additions & 11 deletions src/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ const connect = (...list) => {
};
});

return A => class extends React.Component {
constructor(props) {
super(props);
this.index = array.push(() => this.setState({})) - 1;
this.state = {};
return (A) => {
if (React.PureComponent && A.prototype instanceof React.PureComponent) {
throw new Error('bbx found that you are using "connect" and the PureComponent is passed, don\'t connect a PureComponent.');
}
return class extends React.Component {
constructor(props) {
super(props);
this.index = array.push(() => this.setState({})) - 1;
this.state = {};
}

componentWillUnmount() {
array.splice(this.index, 1);
}
componentWillUnmount() {
array.splice(this.index, 1);
}

render() {
return <A {...this.props} />;
}
render() {
return <A {...this.props} />;
}
};
};
};

Expand Down
16 changes: 16 additions & 0 deletions test/connect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,20 @@ describe('bbx connect', () => {

expect(shallow(<App />).html()).toEqual('<div>lily 1 lily2 2</div>');
});


test.only('connect PureComponent', async () => {
class Data extends State {}
const data = new Data();

class App extends React.PureComponent {
getUser() {}

render() {
return <div>1</div>;
}
}

expect(() => connect(data)(App)).toThrow(new Error('bbx found that you are using "connect" and the PureComponent is passed, don\'t connect a PureComponent.'));
});
});

0 comments on commit aeb1df4

Please sign in to comment.