GeezerVison is showing github.com — Detect React Native v3 backend and show warning · react/react@249a2e0
File treeExpand file treeCollapse file tree Open diff view settings packages/react-devtools-core/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-core/src/standalone.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -86,6 +86,7 @@ function reload() { | 86 | 86 | bridge: ((bridge: any): Bridge), | 87 | 87 | showTabBar: true, | 88 | 88 | store: ((store: any): Store), | | 89 | + warnIfLegacyBackendDetected: true, | 89 | 90 | viewElementSourceFunction, | 90 | 91 | viewElementSourceRequiresFileLocation: true, | 91 | 92 | }) | |
Collapse file Copy file name to clipboardExpand all lines: shells/dev/src/devtools.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,6 +77,7 @@ inject('dist/app.js', () => { | 77 | 77 | browserTheme: 'light', | 78 | 78 | showTabBar: true, | 79 | 79 | store, | | 80 | + warnIfLegacyBackendDetected: true, | 80 | 81 | }) | 81 | 82 | ); | 82 | 83 | batch.then(() => { | |
Collapse file Copy file name to clipboardExpand all lines: src/bridge.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -128,6 +128,12 @@ export default class Bridge extends EventEmitter<{| | 128 | 128 | }) || null; | 129 | 129 | } | 130 | 130 |
| | 131 | + // Listening directly to the wall isn't advised. | | 132 | + // It can be used to listen for legacy (v3) messages (since they use a different format). | | 133 | + get wall(): Wall { | | 134 | + return this._wall; | | 135 | + } | | 136 | + | 131 | 137 | send(event: string, payload: any, transferable?: Array<any>) { | 132 | 138 | if (this._isShutdown) { | 133 | 139 | console.warn( | |
Collapse file Copy file name to clipboardExpand all lines: src/devtools/views/DevTools.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ import ViewElementSourceContext from './Components/ViewElementSourceContext'; | 18 | 18 | import { ProfilerContextController } from './Profiler/ProfilerContext'; | 19 | 19 | import { ModalDialogContextController } from './ModalDialog'; | 20 | 20 | import ReactLogo from './ReactLogo'; | | 21 | +import WarnIfLegacyBackendDetected from './WarnIfLegacyBackendDetected'; | 21 | 22 |
| 22 | 23 | import styles from './DevTools.css'; | 23 | 24 |
| @@ -38,6 +39,7 @@ export type Props = {| | 38 | 39 | defaultTab?: TabID, | 39 | 40 | showTabBar?: boolean, | 40 | 41 | store: Store, | | 42 | + warnIfLegacyBackendDetected?: boolean, | 41 | 43 | viewElementSourceFunction?: ?ViewElementSource, | 42 | 44 | viewElementSourceRequiresFileLocation?: boolean, | 43 | 45 |
| @@ -80,6 +82,7 @@ export default function DevTools({ | 80 | 82 | settingsPortalContainer, | 81 | 83 | showTabBar = false, | 82 | 84 | store, | | 85 | + warnIfLegacyBackendDetected = false, | 83 | 86 | viewElementSourceFunction, | 84 | 87 | viewElementSourceRequiresFileLocation = false, | 85 | 88 | }: Props) { | @@ -143,6 +146,7 @@ export default function DevTools({ | 143 | 146 | </TreeContextController> | 144 | 147 | </ViewElementSourceContext.Provider> | 145 | 148 | </SettingsContextController> | | 149 | + {warnIfLegacyBackendDetected && <WarnIfLegacyBackendDetected />} | 146 | 150 | </ModalDialogContextController> | 147 | 151 | </StoreContext.Provider> | 148 | 152 | </BridgeContext.Provider> | |
Collapse file Copy file name to clipboardExpand all lines: src/devtools/views/ModalDialog.js +21-9Lines changed: 21 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ type DIALOG_ACTION_HIDE = {| | 18 | 18 | |}; | 19 | 19 | type DIALOG_ACTION_SHOW = {| | 20 | 20 | type: 'SHOW', | | 21 | + canBeDismissed?: boolean, | 21 | 22 | content: React$Node, | 22 | 23 | title?: React$Node | null, | 23 | 24 | |}; | @@ -27,6 +28,7 @@ type Action = DIALOG_ACTION_HIDE | DIALOG_ACTION_SHOW; | 27 | 28 | type Dispatch = (action: Action) => void; | 28 | 29 |
| 29 | 30 | type State = {| | | 31 | + canBeDismissed: boolean, | 30 | 32 | content: React$Node | null, | 31 | 33 | isVisible: boolean, | 32 | 34 | title: React$Node | null, | @@ -46,12 +48,14 @@ function dialogReducer(state, action) { | 46 | 48 | switch (action.type) { | 47 | 49 | case 'HIDE': | 48 | 50 | return { | | 51 | + canBeDismissed: true, | 49 | 52 | content: null, | 50 | 53 | isVisible: false, | 51 | 54 | title: null, | 52 | 55 | }; | 53 | 56 | case 'SHOW': | 54 | 57 | return { | | 58 | + canBeDismissed: action.canBeDismissed !== false, | 55 | 59 | content: action.content, | 56 | 60 | isVisible: true, | 57 | 61 | title: action.title || null, | @@ -67,13 +71,15 @@ type Props = {| | 67 | 71 |
| 68 | 72 | function ModalDialogContextController({ children }: Props) { | 69 | 73 | const [state, dispatch] = useReducer<State, Action>(dialogReducer, { | | 74 | + canBeDismissed: true, | 70 | 75 | content: null, | 71 | 76 | isVisible: false, | 72 | 77 | title: null, | 73 | 78 | }); | 74 | 79 |
| 75 | 80 | const value = useMemo<ModalDialogContextType>( | 76 | 81 | () => ({ | | 82 | + canBeDismissed: state.canBeDismissed, | 77 | 83 | content: state.content, | 78 | 84 | isVisible: state.isVisible, | 79 | 85 | title: state.title, | @@ -95,10 +101,14 @@ function ModalDialog(_: {||}) { | 95 | 101 | } | 96 | 102 |
| 97 | 103 | function ModalDialogImpl(_: {||}) { | 98 | | - const { content, dispatch, title } = useContext(ModalDialogContext); | 99 | | - const dismissModal = useCallback(() => dispatch({ type: 'HIDE' }), [ | 100 | | - dispatch, | 101 | | - ]); | | 104 | + const { canBeDismissed, content, dispatch, title } = useContext( | | 105 | + ModalDialogContext | | 106 | + ); | | 107 | + const dismissModal = useCallback(() => { | | 108 | + if (canBeDismissed) { | | 109 | + dispatch({ type: 'HIDE' }); | | 110 | + } | | 111 | + }, [canBeDismissed, dispatch]); | 102 | 112 | const modalRef = useRef<HTMLDivElement | null>(null); | 103 | 113 |
| 104 | 114 | useModalDismissSignal(modalRef, dismissModal); | @@ -108,11 +118,13 @@ function ModalDialogImpl(_: {||}) { | 108 | 118 | <div className={styles.Dialog} ref={modalRef}> | 109 | 119 | {title !== null && <div className={styles.Title}>{title}</div>} | 110 | 120 | {content} | 111 | | - <div className={styles.Buttons}> | 112 | | - <Button autoFocus onClick={dismissModal}> | 113 | | - Okay | 114 | | - </Button> | 115 | | - </div> | | 121 | + {canBeDismissed && ( | | 122 | + <div className={styles.Buttons}> | | 123 | + <Button autoFocus onClick={dismissModal}> | | 124 | + Okay | | 125 | + </Button> | | 126 | + </div> | | 127 | + )} | 116 | 128 | </div> | 117 | 129 | </div> | 118 | 130 | ); | |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +.Command { | | 2 | + background-color: var(--color-dimmest); | | 3 | + padding: 0.25rem 0.5rem; | | 4 | + display: block; | | 5 | + border-radius: 0.125rem; | | 6 | +} |
Collapse file Copy file name to clipboard +60Lines changed: 60 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// @flow | | 2 | + | | 3 | +import React, { Fragment, useContext, useEffect } from 'react'; | | 4 | +import { BridgeContext } from './context'; | | 5 | +import { ModalDialogContext } from './ModalDialog'; | | 6 | + | | 7 | +import styles from './WarnIfLegacyBackendDetected.css'; | | 8 | + | | 9 | +export default function WarnIfLegacyBackendDetected(_: {||}) { | | 10 | + const bridge = useContext(BridgeContext); | | 11 | + const { dispatch } = useContext(ModalDialogContext); | | 12 | + | | 13 | + // Detect pairing with legacy v3 backend. | | 14 | + // We do this by listening to a message that it broadcasts but the v4 backend doesn't. | | 15 | + // In this case the frontend should show upgrade instructions. | | 16 | + useEffect(() => { | | 17 | + // Wall.listen returns a cleanup function | | 18 | + let unlisten = bridge.wall.listen(event => { | | 19 | + switch (event.type) { | | 20 | + case 'call': | | 21 | + case 'event': | | 22 | + case 'many-events': | | 23 | + // Any of these types indicate the v3 backend. | | 24 | + dispatch({ | | 25 | + canBeDismissed: false, | | 26 | + type: 'SHOW', | | 27 | + title: | | 28 | + 'React DevTools v4 is incompatible with this version of React', | | 29 | + content: <InvalidBackendDetected />, | | 30 | + }); | | 31 | + | | 32 | + if (typeof unlisten === 'function') { | | 33 | + unlisten(); | | 34 | + unlisten = null; | | 35 | + } | | 36 | + break; | | 37 | + default: | | 38 | + break; | | 39 | + } | | 40 | + }); | | 41 | + | | 42 | + return () => { | | 43 | + if (typeof unlisten === 'function') { | | 44 | + unlisten(); | | 45 | + unlisten = null; | | 46 | + } | | 47 | + }; | | 48 | + }, [bridge, dispatch]); | | 49 | + | | 50 | + return null; | | 51 | +} | | 52 | + | | 53 | +function InvalidBackendDetected(_: {||}) { | | 54 | + return ( | | 55 | + <Fragment> | | 56 | + <p>Either upgrade React or install React DevTools v3:</p> | | 57 | + <code className={styles.Command}>npm install -d react-devtools@^3</code> | | 58 | + </Fragment> | | 59 | + ); | | 60 | +} |
|