From 2adf37849b7164b8219a0205dc1afa5c18dbd75c Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Wed, 23 Nov 2022 13:15:10 +0100 Subject: [PATCH 1/3] ref: stop using try..catch as control flow in Wrapper Previously, Wrapper used try..catch around a call to useStoreApi to check whether there was an available StoreContext, instead of simply using useContext and checking the value. This leads to poor developer experience when using the devtools feature "pause on caught exceptions" as it causes the devtools to break on every render of a Wrapper that is not within a StoreContext provider. The catch is also universal, meaning that it could catch a completely unrelated error and happily keep going instead of crashing which is undesirable. This commit changes this try..catch into a simple call to useContext to check if a StoreContext is available. --- packages/core/src/container/ReactFlow/Wrapper.tsx | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/packages/core/src/container/ReactFlow/Wrapper.tsx b/packages/core/src/container/ReactFlow/Wrapper.tsx index 297e3b49..b6069948 100644 --- a/packages/core/src/container/ReactFlow/Wrapper.tsx +++ b/packages/core/src/container/ReactFlow/Wrapper.tsx @@ -1,18 +1,10 @@ -import type { FC, PropsWithChildren } from 'react'; +import { FC, PropsWithChildren, useContext } from 'react'; -import { useStoreApi } from '../../hooks/useStore'; +import StoreContext from '../../contexts/RFStoreContext'; import ReactFlowProvider from '../../components/ReactFlowProvider'; const Wrapper: FC = ({ children }) => { - let isWrapped = true; - - try { - useStoreApi(); - } catch (e) { - isWrapped = false; - } - - if (isWrapped) { + if (useContext(StoreContext)) { // we need to wrap it with a fragment because it's not allowed for children to be a ReactNode // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18051 return <>{children}; From 22db82c8b8ab7d9feeb20fdedc6b50bb7479af4c Mon Sep 17 00:00:00 2001 From: moklick Date: Fri, 25 Nov 2022 22:03:48 +0100 Subject: [PATCH 2/3] chore(wrapper): cleanup --- packages/core/src/container/ReactFlow/Wrapper.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/core/src/container/ReactFlow/Wrapper.tsx b/packages/core/src/container/ReactFlow/Wrapper.tsx index b6069948..f0f56d24 100644 --- a/packages/core/src/container/ReactFlow/Wrapper.tsx +++ b/packages/core/src/container/ReactFlow/Wrapper.tsx @@ -1,10 +1,13 @@ -import { FC, PropsWithChildren, useContext } from 'react'; +import { useContext } from 'react'; +import type { FC, PropsWithChildren } from 'react'; import StoreContext from '../../contexts/RFStoreContext'; import ReactFlowProvider from '../../components/ReactFlowProvider'; const Wrapper: FC = ({ children }) => { - if (useContext(StoreContext)) { + const isWrapped = useContext(StoreContext); + + if (isWrapped) { // we need to wrap it with a fragment because it's not allowed for children to be a ReactNode // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18051 return <>{children}; From b0302ce4261a992bee841bae84af347d03be690f Mon Sep 17 00:00:00 2001 From: moklick Date: Fri, 25 Nov 2022 22:05:51 +0100 Subject: [PATCH 3/3] chore(changeset): add --- .changeset/perfect-spiders-own.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/perfect-spiders-own.md diff --git a/.changeset/perfect-spiders-own.md b/.changeset/perfect-spiders-own.md new file mode 100644 index 00000000..d565e109 --- /dev/null +++ b/.changeset/perfect-spiders-own.md @@ -0,0 +1,5 @@ +--- +'@reactflow/core': patch +--- + +Wrapper: dont use try catch for checking if provider is available