Skip to content

Commit

Permalink
<feat>: add ConstateProvider.FromValue diegohaz#127
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyun authored and liuyun committed May 14, 2021
1 parent a5f42da commit 06601c3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Provider<Props, Value> = React.FC<Props> & {
useProvider: (
props?: Props
) => [(element: React.ReactNode) => React.ReactElement, Value];
FromValue: React.FC<{ value: Value }>;
};

// const [Provider, useCounterContext] = constate(...)
Expand Down Expand Up @@ -70,7 +71,7 @@ export function constate<Props, Value, Selectors extends Selector<Value>[]>(
createContext(useValue.name);
}

const ConstateProviderBase: React.FC<{ value: Value }> = ({
const ConstateProviderFromValue: React.FC<{ value: Value }> = ({
value,
children,
}) => {
Expand All @@ -87,18 +88,22 @@ export function constate<Props, Value, Selectors extends Selector<Value>[]>(

const ConstateProvider: Provider<Props, Value> = ({ children, ...props }) => {
return (
<ConstateProviderBase value={useValue(props as Props)}>
<ConstateProviderFromValue value={useValue(props as Props)}>
{children}
</ConstateProviderBase>
</ConstateProviderFromValue>
);
};

ConstateProvider.FromValue = ConstateProviderFromValue;

ConstateProvider.useProvider = (props?: Props) => {
const value = useValue(props!);

const inject = (element: React.ReactNode) => {
return (
<ConstateProviderBase value={value}>{element}</ConstateProviderBase>
<ConstateProviderFromValue value={value}>
{element}
</ConstateProviderFromValue>
);
};

Expand All @@ -108,6 +113,7 @@ export function constate<Props, Value, Selectors extends Selector<Value>[]>(
if (process.env.NODE_ENV !== "production") {
if (useValue.name) {
ConstateProvider.displayName = "Constate";
ConstateProviderFromValue.displayName = "ConstateFromValue";
}
}

Expand Down

0 comments on commit 06601c3

Please sign in to comment.