Skip to content

Commit

Permalink
fix: export WithStoresProps
Browse files Browse the repository at this point in the history
  • Loading branch information
ddadaal committed Feb 28, 2019
1 parent 49c6558 commit 20355d2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function CounterWithHook() {
function ComponentWithRenderProps() {
return (
<StoreConsumer>
{({ useStore }) => { // inject only `useStore` with the same signature and usage as above
{({ useStore }: ConsumerActions) => { // inject a ConsumerActions object. Explicitly specifying type is not required.
const store = useStore(TestStore);
return (
<span>
Expand All @@ -75,7 +75,11 @@ function ComponentWithRenderProps() {
}

// 3.2 Use HOC
const ComponentWithHOC = withStores(({ useStore }) => (

// extends WithStoresProps to get `useStore` function
interface Props extends WithStoresProps { }

const ComponentWithHOC = withStores(({ useStore }: Props) => (
<span>{useStore(TestStore).state.value}</span>
));

Expand Down Expand Up @@ -205,7 +209,7 @@ class BStore extends Store<{ text: string; derived: string; }> {
- [X] Add test
- [X] Achieve high test coverage
- [X] Implement `setState` promise resolve after component update for hook use
- [ ] [Partial observer](https://github.com/viccrubs/simstate/blob/partial-observer/partial-observer-proposal.md)
- [X] [Partial observer](https://github.com/viccrubs/simstate/blob/partial-observer/partial-observer-proposal.md)
- [ ] SSR utilities and its example

# Related Articles
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
"tslib": "^1.9.3"
},
"peerDependencies": {
"react": ">=16.8.0"
"react": ">=16.8.3"
},
"devDependencies": {
"@types/enzyme": "^3.9.0",
"@types/jest": "^24.0.6",
"@types/react": ">=16.8.0",
"@types/jest": "^24.0.9",
"@types/react": ">=16.8.5",
"coveralls": "^3.0.3",
"cross-env": "^5.2.0",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.9.1",
"enzyme-adapter-react-16": "^1.10.0",
"jest": "^24.1.0",
"react": ">=16.8.0",
"react": ">=16.8.3",
"react-dom": "^16.8.3",
"rimraf": "^2.6.3",
"rollup": "latest",
Expand All @@ -50,7 +50,7 @@
"rollup-plugin-serve": "latest",
"rollup-plugin-typescript2": "latest",
"rollup-watch": "latest",
"standard-version": "^5.0.0",
"standard-version": "^5.0.1",
"ts-jest": "^24.0.0",
"tslint": "latest",
"tslint-react": "^3.6.0",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export { useStores };
import StoreConsumer, { ConsumerActions } from "./StoreConsumer";
export { StoreConsumer, ConsumerActions };

import withStores from "./withStores";
export { withStores };
import withStores, { WithStoresProps } from "./withStores";
export { withStores, WithStoresProps };
6 changes: 4 additions & 2 deletions src/withStores.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React from "react";
import StoreConsumer, { ConsumerActions } from "./StoreConsumer";
import { Omit } from "./types";

export type WithStoresProps = ConsumerActions;

/**
* Higher-Order component usage
* @param WrappedComponent
*/
export default function withStores
<P extends {}>(WrappedComponent: React.ComponentType<P & ConsumerActions>) {
<P extends {}>(WrappedComponent: React.ComponentType<P & WithStoresProps>) {
const Component = (props: P) => (
<StoreConsumer>
{(actions) => {
Expand All @@ -21,5 +23,5 @@ export default function withStores
</StoreConsumer>
);

return Component as unknown as React.ComponentType<Omit<P, keyof ConsumerActions>>;
return Component as unknown as React.ComponentType<Omit<P, keyof WithStoresProps>>;
}
8 changes: 6 additions & 2 deletions tests/hoc.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { TestStore } from "./common";
import React from "react";
import { mount } from "enzyme";
import withStores from "../src/withStores";
import withStores, { WithStoresProps } from "../src/withStores";
import StoreProvider from "../src/StoreProvider";

interface Props extends WithStoresProps {

}

describe("HOC", () => {

const Component = withStores(({ useStore }) => {
const Component = withStores(({ useStore }: Props) => {
const store = useStore(TestStore);
return (
<span>{store.state.value}</span>
Expand Down

0 comments on commit 20355d2

Please sign in to comment.