Skip to content

Commit

Permalink
fix: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
MinJieLiu committed Dec 7, 2021
1 parent 5b3e561 commit 24893f7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
22 changes: 6 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,23 @@ const Container = createContainer(useCustomHook);

### `<Container.Provider>`

Container.Provider基本用法
Container.Provider 基本用法

```tsx
function ParentComponent() {
function Parent() {
return (
<Container.Provider>
<ChildComponent />
</Container.Provider>
);
}
```

Container.Provider支持Function的children

```tsx
function useCustomHook() {
const [value, setValue] = useState();
return { value };
}

function ParentComponent() {
function Parent1() {
return (
<Container.Provider>
{({ value })=>{
// use value to do something in top component.
// const computedValue=doSomething(value);
return <ChildComponent />
{(value) => {
// const computed = doSomething(value);
return <ChildComponent />;
}}
</Container.Provider>
);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "heo",
"version": "1.3.0",
"version": "1.4.0",
"description": "A simple and easy to use react state management library",
"author": "MinJieLiu",
"license": "MIT",
Expand Down Expand Up @@ -42,7 +42,6 @@
"@types/react-dom": "^16.9.9",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/parser": "^4.6.1",
"microbundle-crl": "^0.13.11",
"babel-eslint": "^10.1.0",
"cross-env": "^7.0.2",
"eslint": "^6.8.0",
Expand All @@ -52,6 +51,7 @@
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"gh-pages": "^3.1.0",
"microbundle-crl": "^0.13.11",
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"react": "^16.13.1",
Expand Down
11 changes: 6 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

export type SelectorFn<Value, Selected> = (value: Value) => Selected;

export interface ContainerProviderProps<State = void,Value=void> {
export interface ContainerProviderProps<State = void, Value = void> {
initialState?: State;
children?: React.ReactNode | ((value: Value) => React.ReactNode);
}
Expand All @@ -22,8 +22,8 @@ export function createContainer<Value, State = void>(useHook: (initialState?: St
const Context = React.createContext<Value | null>(null, () => 0);
const ListenerContext = React.createContext<Set<(value: Value) => void>>(new Set());

const Provider: React.FC<ContainerProviderProps<State,Value>> = React.memo(
({ initialState, children }) => {
const Provider = React.memo(
({ initialState, children }: ContainerProviderProps<State, Value>) => {
const value = useHook(initialState);
const listeners = React.useRef<Set<(listener: Value) => void>>(new Set()).current;

Expand All @@ -38,11 +38,12 @@ export function createContainer<Value, State = void>(useHook: (initialState?: St
listener(value);
});
}
const wrapChildren = typeof children === "function" ? children(value) : children;

return (
<Context.Provider value={value}>
<ListenerContext.Provider value={listeners}>{wrapChildren}</ListenerContext.Provider>
<ListenerContext.Provider value={listeners}>
{typeof children === 'function' ? children(value) : children}
</ListenerContext.Provider>
</Context.Provider>
);
},
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3147,9 +3147,9 @@ caniuse-api@^3.0.0:
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001035, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001154:
version "1.0.30001156"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001156.tgz#75c20937b6012fe2b02ab58b30d475bf0718de97"
integrity sha512-z7qztybA2eFZTB6Z3yvaQBIoJpQtsewRD74adw2UbRWwsRq3jIPvgrQGawBMbfafekQaD21FWuXNcywtTDGGCw==
version "1.0.30001285"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001285.tgz"
integrity sha512-KAOkuUtcQ901MtmvxfKD+ODHH9YVDYnBt+TGYSz2KIfnq22CiArbUxXPN9067gNbgMlnNYRSwho8OPXZPALB9Q==

capture-exit@^2.0.0:
version "2.0.0"
Expand Down

0 comments on commit 24893f7

Please sign in to comment.