Skip to content

Commit

Permalink
fix: useContextSelector context TS type.
Browse files Browse the repository at this point in the history
  • Loading branch information
abouolia committed Jan 16, 2023
1 parent 9513df8 commit e695522
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/basic/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { createNextContext, useContextSelector } from 'use-context-next';
import { createNextContext, useContextSelector } from '../../../src';
import './App.css';

interface Value {
Expand Down
15 changes: 2 additions & 13 deletions src/ContextNextProvider.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import React, {
import {
createContext,
createElement,
useEffect,
useRef,
Provider,
ReactNode,
Context,
} from 'react';
import { ContextNextValue, Listener } from './types';
import { ContextNextValue, Listener, NextContext } from './types';

const PROVIDER_NAME = '@use-context-next';
const ORIGINAL_PROVIDER = Symbol();

type NextContextProvider<T> = (props: {
children: ReactNode;
value: T;
}) => React.FunctionComponentElement<React.ProviderProps<ContextNextValue<T>>>;

interface NextContext<T> extends Omit<Context<T>, 'Provider'> {
Provider: NextContextProvider<T>;
ORIGINAL_PROVIDER: NextContextProvider<T>;
}

export const createNextContext = <Value>(defaultValue: Value) => {
const Context = createContext<ContextNextValue<Value>>({
value: defaultValue,
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import React, { ReactNode, Context } from 'react';

export type Listener = ({ value }: any) => void;
export type Listeners = Set<Listener>;
export type ContextNextValue<T> = {
value: T;
listeners: Listeners;
};

export type NextContextProvider<T> = (props: {
children: ReactNode;
value: T;
}) => React.FunctionComponentElement<React.ProviderProps<ContextNextValue<T>>>;

export interface NextContext<T> extends Omit<Context<T>, 'Provider'> {
Provider: NextContextProvider<T>;
ORIGINAL_PROVIDER: NextContextProvider<T>;
}
9 changes: 5 additions & 4 deletions src/useContextSelector.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React, { useContext, useState } from 'react';
import { ContextNextValue } from './types';
import { ContextNextValue, NextContext } from './types';
import { useIsomorphicLayoutEffect } from './utils';

export const useContextSelector = <Value, Output>(
context: React.Context<ContextNextValue<Value>>,
context: NextContext<Value>,
selector: (value: Value) => Output,
comparator: (value1: any, value2: any) => boolean = Object.is
): Output => {
const contextValue = useContext<ContextNextValue<Value>>(context);

const contextValue = useContext<ContextNextValue<Value>>(
context as unknown as React.Context<ContextNextValue<Value>>
);
const initialValue = selector(contextValue.value);
const [state, setState] = useState<Output>(initialValue);

Expand Down

0 comments on commit e695522

Please sign in to comment.