Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @module NiceModal
* */

import React, { useEffect, useCallback, useContext, useReducer, useMemo, ReactNode } from 'react';
import React, { useEffect, useCallback, useContext, useReducer, useMemo, ReactNode, useRef } from 'react';

export interface NiceModalState {
id: string;
Expand Down Expand Up @@ -478,7 +478,15 @@ const NiceModalPlaceholder: React.FC = () => {
const InnerContextProvider: React.FC = ({ children }) => {
const arr = useReducer(reducer, initialState);
const modals = arr[0];
dispatch = arr[1];
// why not write `fnRef.current = fn`? https://github.com/alibaba/hooks/issues/728
const fnRef = useRef<any>();
fnRef.current = useMemo<any>(() => {
return function innerDispatch(action: any) {
(arr[1] as React.Dispatch<any>)(action);
};
}, [arr]);

dispatch = fnRef.current;
return (
<NiceModalContext.Provider value={modals}>
{children}
Expand Down