Skip to content

Commit 0b5357c

Browse files
committed
add manual mode
1 parent 447f5f2 commit 0b5357c

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

example/csr/src/App.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
import React, { useEffect, useReducer, useState, useContext } from "react";
2-
import ContextDevTool from "react-context-devtool";
2+
// import ContextDevTool from "react-context-devtool";
3+
4+
5+
export const ContextDevTool = ({
6+
id,
7+
context: Context,
8+
displayName,
9+
}) => {
10+
return (
11+
<Context.Consumer>
12+
{(values) => {
13+
if (typeof window !== "undefined" && window.__REACT_CONTEXT_DEVTOOL_GLOBAL_HOOK) {
14+
window.__REACT_CONTEXT_DEVTOOL_GLOBAL_HOOK.debug({ id, displayName, values });
15+
}
16+
return null;
17+
}}
18+
</Context.Consumer>
19+
);
20+
};
21+
322

423
const MyContext1 = React.createContext({});
524
const MyContext2 = React.createContext({});
@@ -179,9 +198,9 @@ class App extends React.Component {
179198
{/* {
180199
new Array(6000).fill().map((d, ind) => <div>{ind}</div>)
181200
} */}
182-
{/* <MyContext1.Provider value={{ d: 123 , id: counter1 }} sdisplayName="test"> */}
201+
<MyContext1.Provider value={{ d: 123 , id: counter1 }} sdisplayName="test">
183202
{/* <> */}
184-
{/* <button onClick={() => this.setState({ counter1: counter1 + 1 })}>Click Me {counter1}</button> */}
203+
<button onClick={() => this.setState({ counter1: counter1 + 1 })}>Click Me {counter1}</button>
185204
{/* <button onClick={() => changeValue({type: 'increment'})}>ddddddddddddddddddddd</button> */}
186205
<Counter />
187206
{/* <div id="root1">ssss</div> */}
@@ -192,13 +211,13 @@ class App extends React.Component {
192211
counter1 < 5 && <Test />
193212
} */}
194213
<Test2 id={counter1} />
195-
{/* <ContextDevTool
214+
<ContextDevTool
196215
context={MyContext1}
197216
id="cont2"
198217
displayName="Demo Context"
199-
/> */}
218+
/>
200219
{/* </> */}
201-
{/* </MyContext1.Provider> */}
220+
</MyContext1.Provider>
202221
</>
203222
);
204223
}

packages/devtool-extenstion/extension/core/injectGlobalHook.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ function injectHelpers(target) {
109109
};
110110
}
111111

112+
function manualScriptToInject() {
113+
window.__REACT_CONTEXT_DEVTOOL_GLOBAL_HOOK.debug = (data) => {
114+
const helpers = window.__REACT_CONTEXT_DEVTOOL_GLOBAL_HOOK.helpers;
115+
window.postMessage(
116+
{
117+
type: "__REACT_CONTEXT_DEVTOOL_GLOBAL_HOOK_EVENT",
118+
subType: "ADD_APP_DATA",
119+
data: helpers.parseData({
120+
context: {
121+
[data.id]: {
122+
displayName: data.displayName,
123+
value: data.values,
124+
valueChanged: true,
125+
remove: false,
126+
},
127+
},
128+
}),
129+
},
130+
"*"
131+
);
132+
};
133+
}
134+
112135
/**
113136
* use react devtool internals for debug
114137
*
@@ -169,6 +192,7 @@ chrome.runtime.sendMessage(
169192
injectCode(`
170193
${initHook}
171194
;(${injectHelpers.toString()}(window))
195+
;(${manualScriptToInject.toString()}(window))
172196
;(${installHook.toString()}(window, ${settings}))
173197
`);
174198
}

packages/devtool-extenstion/src/containers/Sidebar/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ const Sidebar = () => {
8282
<div className="filters">
8383
{settings.debugUseReducer ? <Badge>useReducers List</Badge> : null}
8484
{settings.debugContext ? <Badge>Context List</Badge> : null}
85+
{!settings.debugContext && !settings.debugUseReducer ? (
86+
<Badge>Context List</Badge>
87+
) : null}
8588
</div>
8689
)}
8790
<ul className="debuglist">

0 commit comments

Comments
 (0)