Best JavaScript code snippet using playwright-internal
ReactFiberHooks.js
Source:ReactFiberHooks.js
...1145 mountHookTypesDev();1146 const prevDispatcher = ReactCurrentDispatcher.current;1147 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnMountInDEV;1148 try {1149 return mountMemo(create, deps);1150 } finally {1151 ReactCurrentDispatcher.current = prevDispatcher;1152 }1153 },1154 useReducer<S, I, A>(1155 reducer: (S, A) => S,1156 initialArg: I,1157 init?: I => S,1158 ): [S, Dispatch<A>] {1159 currentHookNameInDev = 'useReducer';1160 mountHookTypesDev();1161 const prevDispatcher = ReactCurrentDispatcher.current;1162 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnMountInDEV;1163 try {1164 return mountReducer(reducer, initialArg, init);1165 } finally {1166 ReactCurrentDispatcher.current = prevDispatcher;1167 }1168 },1169 useRef<T>(initialValue: T): {current: T} {1170 currentHookNameInDev = 'useRef';1171 mountHookTypesDev();1172 return mountRef(initialValue);1173 },1174 useState<S>(1175 initialState: (() => S) | S,1176 ): [S, Dispatch<BasicStateAction<S>>] {1177 currentHookNameInDev = 'useState';1178 mountHookTypesDev();1179 const prevDispatcher = ReactCurrentDispatcher.current;1180 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnMountInDEV;1181 try {1182 return mountState(initialState);1183 } finally {1184 ReactCurrentDispatcher.current = prevDispatcher;1185 }1186 },1187 useDebugValue<T>(value: T, formatterFn: ?(value: T) => mixed): void {1188 currentHookNameInDev = 'useDebugValue';1189 mountHookTypesDev();1190 return mountDebugValue(value, formatterFn);1191 },1192 };1193 HooksDispatcherOnMountWithHookTypesInDEV = {1194 readContext<T>(1195 context: ReactContext<T>,1196 observedBits: void | number | boolean,1197 ): T {1198 return readContext(context, observedBits);1199 },1200 useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {1201 currentHookNameInDev = 'useCallback';1202 updateHookTypesDev();1203 return mountCallback(callback, deps);1204 },1205 useContext<T>(1206 context: ReactContext<T>,1207 observedBits: void | number | boolean,1208 ): T {1209 currentHookNameInDev = 'useContext';1210 updateHookTypesDev();1211 return readContext(context, observedBits);1212 },1213 useEffect(1214 create: () => (() => void) | void,1215 deps: Array<mixed> | void | null,1216 ): void {1217 currentHookNameInDev = 'useEffect';1218 updateHookTypesDev();1219 return mountEffect(create, deps);1220 },1221 useImperativeHandle<T>(1222 ref: {current: T | null} | ((inst: T | null) => mixed) | null | void,1223 create: () => T,1224 deps: Array<mixed> | void | null,1225 ): void {1226 currentHookNameInDev = 'useImperativeHandle';1227 updateHookTypesDev();1228 return mountImperativeHandle(ref, create, deps);1229 },1230 useLayoutEffect(1231 create: () => (() => void) | void,1232 deps: Array<mixed> | void | null,1233 ): void {1234 currentHookNameInDev = 'useLayoutEffect';1235 updateHookTypesDev();1236 return mountLayoutEffect(create, deps);1237 },1238 useMemo<T>(create: () => T, deps: Array<mixed> | void | null): T {1239 currentHookNameInDev = 'useMemo';1240 updateHookTypesDev();1241 const prevDispatcher = ReactCurrentDispatcher.current;1242 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnMountInDEV;1243 try {1244 return mountMemo(create, deps);1245 } finally {1246 ReactCurrentDispatcher.current = prevDispatcher;1247 }1248 },1249 useReducer<S, I, A>(1250 reducer: (S, A) => S,1251 initialArg: I,1252 init?: I => S,1253 ): [S, Dispatch<A>] {1254 currentHookNameInDev = 'useReducer';1255 updateHookTypesDev();1256 const prevDispatcher = ReactCurrentDispatcher.current;1257 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnMountInDEV;1258 try {1259 return mountReducer(reducer, initialArg, init);1260 } finally {1261 ReactCurrentDispatcher.current = prevDispatcher;1262 }1263 },1264 useRef<T>(initialValue: T): {current: T} {1265 currentHookNameInDev = 'useRef';1266 updateHookTypesDev();1267 return mountRef(initialValue);1268 },1269 useState<S>(1270 initialState: (() => S) | S,1271 ): [S, Dispatch<BasicStateAction<S>>] {1272 currentHookNameInDev = 'useState';1273 updateHookTypesDev();1274 const prevDispatcher = ReactCurrentDispatcher.current;1275 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnMountInDEV;1276 try {1277 return mountState(initialState);1278 } finally {1279 ReactCurrentDispatcher.current = prevDispatcher;1280 }1281 },1282 useDebugValue<T>(value: T, formatterFn: ?(value: T) => mixed): void {1283 currentHookNameInDev = 'useDebugValue';1284 updateHookTypesDev();1285 return mountDebugValue(value, formatterFn);1286 },1287 };1288 HooksDispatcherOnUpdateInDEV = {1289 readContext<T>(1290 context: ReactContext<T>,1291 observedBits: void | number | boolean,1292 ): T {1293 return readContext(context, observedBits);1294 },1295 useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {1296 currentHookNameInDev = 'useCallback';1297 updateHookTypesDev();1298 return updateCallback(callback, deps);1299 },1300 useContext<T>(1301 context: ReactContext<T>,1302 observedBits: void | number | boolean,1303 ): T {1304 currentHookNameInDev = 'useContext';1305 updateHookTypesDev();1306 return readContext(context, observedBits);1307 },1308 useEffect(1309 create: () => (() => void) | void,1310 deps: Array<mixed> | void | null,1311 ): void {1312 currentHookNameInDev = 'useEffect';1313 updateHookTypesDev();1314 return updateEffect(create, deps);1315 },1316 useImperativeHandle<T>(1317 ref: {current: T | null} | ((inst: T | null) => mixed) | null | void,1318 create: () => T,1319 deps: Array<mixed> | void | null,1320 ): void {1321 currentHookNameInDev = 'useImperativeHandle';1322 updateHookTypesDev();1323 return updateImperativeHandle(ref, create, deps);1324 },1325 useLayoutEffect(1326 create: () => (() => void) | void,1327 deps: Array<mixed> | void | null,1328 ): void {1329 currentHookNameInDev = 'useLayoutEffect';1330 updateHookTypesDev();1331 return updateLayoutEffect(create, deps);1332 },1333 useMemo<T>(create: () => T, deps: Array<mixed> | void | null): T {1334 currentHookNameInDev = 'useMemo';1335 updateHookTypesDev();1336 const prevDispatcher = ReactCurrentDispatcher.current;1337 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnUpdateInDEV;1338 try {1339 return updateMemo(create, deps);1340 } finally {1341 ReactCurrentDispatcher.current = prevDispatcher;1342 }1343 },1344 useReducer<S, I, A>(1345 reducer: (S, A) => S,1346 initialArg: I,1347 init?: I => S,1348 ): [S, Dispatch<A>] {1349 currentHookNameInDev = 'useReducer';1350 updateHookTypesDev();1351 const prevDispatcher = ReactCurrentDispatcher.current;1352 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnUpdateInDEV;1353 try {1354 return updateReducer(reducer, initialArg, init);1355 } finally {1356 ReactCurrentDispatcher.current = prevDispatcher;1357 }1358 },1359 useRef<T>(initialValue: T): {current: T} {1360 currentHookNameInDev = 'useRef';1361 updateHookTypesDev();1362 return updateRef(initialValue);1363 },1364 useState<S>(1365 initialState: (() => S) | S,1366 ): [S, Dispatch<BasicStateAction<S>>] {1367 currentHookNameInDev = 'useState';1368 updateHookTypesDev();1369 const prevDispatcher = ReactCurrentDispatcher.current;1370 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnUpdateInDEV;1371 try {1372 return updateState(initialState);1373 } finally {1374 ReactCurrentDispatcher.current = prevDispatcher;1375 }1376 },1377 useDebugValue<T>(value: T, formatterFn: ?(value: T) => mixed): void {1378 currentHookNameInDev = 'useDebugValue';1379 updateHookTypesDev();1380 return updateDebugValue(value, formatterFn);1381 },1382 };1383 InvalidNestedHooksDispatcherOnMountInDEV = {1384 readContext<T>(1385 context: ReactContext<T>,1386 observedBits: void | number | boolean,1387 ): T {1388 warnInvalidContextAccess();1389 return readContext(context, observedBits);1390 },1391 useCallback<T>(callback: T, deps: Array<mixed> | void | null): T {1392 currentHookNameInDev = 'useCallback';1393 warnInvalidHookAccess();1394 mountHookTypesDev();1395 return mountCallback(callback, deps);1396 },1397 useContext<T>(1398 context: ReactContext<T>,1399 observedBits: void | number | boolean,1400 ): T {1401 currentHookNameInDev = 'useContext';1402 warnInvalidHookAccess();1403 mountHookTypesDev();1404 return readContext(context, observedBits);1405 },1406 useEffect(1407 create: () => (() => void) | void,1408 deps: Array<mixed> | void | null,1409 ): void {1410 currentHookNameInDev = 'useEffect';1411 warnInvalidHookAccess();1412 mountHookTypesDev();1413 return mountEffect(create, deps);1414 },1415 useImperativeHandle<T>(1416 ref: {current: T | null} | ((inst: T | null) => mixed) | null | void,1417 create: () => T,1418 deps: Array<mixed> | void | null,1419 ): void {1420 currentHookNameInDev = 'useImperativeHandle';1421 warnInvalidHookAccess();1422 mountHookTypesDev();1423 return mountImperativeHandle(ref, create, deps);1424 },1425 useLayoutEffect(1426 create: () => (() => void) | void,1427 deps: Array<mixed> | void | null,1428 ): void {1429 currentHookNameInDev = 'useLayoutEffect';1430 warnInvalidHookAccess();1431 mountHookTypesDev();1432 return mountLayoutEffect(create, deps);1433 },1434 useMemo<T>(create: () => T, deps: Array<mixed> | void | null): T {1435 currentHookNameInDev = 'useMemo';1436 warnInvalidHookAccess();1437 mountHookTypesDev();1438 const prevDispatcher = ReactCurrentDispatcher.current;1439 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnMountInDEV;1440 try {1441 return mountMemo(create, deps);1442 } finally {1443 ReactCurrentDispatcher.current = prevDispatcher;1444 }1445 },1446 useReducer<S, I, A>(1447 reducer: (S, A) => S,1448 initialArg: I,1449 init?: I => S,1450 ): [S, Dispatch<A>] {1451 currentHookNameInDev = 'useReducer';1452 warnInvalidHookAccess();1453 mountHookTypesDev();1454 const prevDispatcher = ReactCurrentDispatcher.current;1455 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnMountInDEV;...
learn-react-hook.js
Source:learn-react-hook.js
1// 以setState为ä¾åæreact-hookæºç ç¥è¯2// è¿éuseStateå
¶å®æ¯dispatcher对象éé¢çä¸ä¸ªæ¹æ³3export function useState < S > (initialState: (() => S) | S) {4 const dispatcher = resolveDispatcher();5 return dispatcher.useState(initialState);6}7// 继ç»è·è¸ªdispatcherå³resolveDispatcher()çè¿åå¼8function resolveDispatcher() {9 //主è¦è¿æ¯ä½¿ç¨çReactCurrentDispatcherè¿ä¸ªå¯¹è±¡çå¼10 const dispatcher = ReactCurrentDispatcher.current;11 //...12 return dispatcher;13}14// 继ç»è·ä¸ReactCurrentDispatcher15const ReactCurrentDispatcher = {16 /**17 * @internal18 * @type {ReactComponent}19 */20 // 追寻å°å
¶å®æå使ç¨çè¿æ¯reactèªèº«æä¾çDispatcher,æç»æåçè¿æ¯æ°çåè°å¨ä¸çFiberHooks21 // import type {Dispatcher} from 'react-reconciler/src/ReactFiberHooks';22 current: (null: null | Dispatcher),23};24// 继ç»è·è¿Dispatcher path: 'react-reconciler/src/ReactFiberHooks'25// è¿å
¥Dispatcher åç°Dispatcherççå½éé¢ä¸å¤§å ç¸å
³çhook,å
¨é¨é½å¨è¿éå®ä¹å¥½äºï¼æ¾å°useStateç声æ26export type Dispatcher = {27 readContext<T>(28 context: ReactContext<T>,29 observedBits: void | number | boolean,30 ): T,31 // useStateå®ä¹çæ ¼å¼ï¼ä¸ä¸ªè¿å
¥çæ³åSæ¯å½æ°æè
ä¸ä¸ªå¼ï¼åæ¶è¿åä¸ä¸ªS以åå¨ä½ç¨äºæ´æ°S32 useState<S>(initialState: (() => S) | S): [S, Dispatch<BasicStateAction<S>>],33 useReducer<S, I, A>(34 reducer: (S, A) => S,35 initialArg: I,36 init?: (I) => S,37 ): [S, Dispatch<A>],38 useContext<T>(39 context: ReactContext<T>,40 observedBits: void | number | boolean,41 ): T,42 useRef<T>(initialValue: T): {current: T},43 useEffect(44 create: () => (() => void) | void,45 deps: Array<mixed> | void | null,46 ): void,47 useLayoutEffect(48 create: () => (() => void) | void,49 deps: Array<mixed> | void | null,50 ): void,51 useCallback<T>(callback: T, deps: Array<mixed> | void | null): T,52 useMemo<T>(nextCreate: () => T, deps: Array<mixed> | void | null): T,53 useImperativeHandle<T>(54 ref: {current: T | null} | ((inst: T | null) => mixed) | null | void,55 create: () => T,56 deps: Array<mixed> | void | null,57 ): void,58 useDebugValue<T>(value: T, formatterFn: ?(value: T) => mixed): void,59};60// è·è¿Dispatcherçæ£çé»è¾å®ä½,åç°æåç§åæ ·çå®ä½ä½¿ç¨å°äºDispatcherçå®ä¹å¨æ¤éæ©hookæ载以åhookæ´æ°çç¶æå®ä½å»åæ61//Hooks æè½½å¨ç»ä»¶æ¶çæ
å½¢, å
¨é½æ¯mountç¶æ62const HooksDispatcherOnMount: Dispatcher = {63 readContext,64 useCallback: mountCallback,65 useContext: readContext,66 useEffect: mountEffect,67 useImperativeHandle: mountImperativeHandle,68 useLayoutEffect: mountLayoutEffect,69 useMemo: mountMemo,70 useReducer: mountReducer,71 useRef: mountRef,72 //useStateå¨mountç¶æçæ¶å对åºçmountStateè¿è¡è·è¸ª73 useState: mountState,74 useDebugValue: mountDebugValue,75};76// mountState ç¸å
³ç代ç 77function mountState<S>(78 initialState: (() => S) | S,79): [S, Dispatch<BasicStateAction<S>>] {80 // å®ä¹ä¸ä¸ªhookçä¸è¥¿ï¼è¿ä¸ªhookä¸ç¥éæ¯ä¸ªä»ä¹ä¸è¥¿ï¼å¥½åæ¯ä»workInProgressè¿ä¸ªFiberæ éé¢æ¿åºæ¥çä¸è¥¿81 const hook = mountWorkInProgressHook();82 if (typeof initialState === 'function') {83 // æ°æ§åå§ååå§ç¶æ84 initialState = initialState();85 }86 // å½åhookè®°ä½çç¶æmomoizedState = initialStateå³åå§åstateçå¼87 hook.memoizedState = hook.baseState = initialState;88 // å®ä¹ä¸ä¸ªhookçéå89 const queue = (hook.queue = {90 last: null, // ä¸æç½æ¯ä»ä¹ä¸è¥¿91 dispatch: null, // è¿ä¸ªæè§æç¹åç¶æ管ççå¨ä½è§¦åå¨92 lastRenderedReducer: basicStateReducer, //ä¸ç¥éä»ä¹ä¸è¥¿ï¼æ ¹æ®å½åï¼å¯ä»¥è®¤ä¸ºæ¯ä¸ä¸æ¬¡æ¸²æçrenducer93 lastRenderedState: (initialState: any), // è¿ä¸ªåºè¯¥æ¯ä¸ä¸æ¬¡æ¸²æçstateå¦94 });95 const dispatch: Dispatch<96 BasicStateAction<S>,97 > = (queue.dispatch = (dispatchAction.bind(98 null,99 // Flow doesn't know this is non-null, but we do.100 ((currentlyRenderingFiber: any): Fiber),101 queue,102 ): any)); // è¿éhook.queue.dispatchå
¶å®å°±æ¯reactéé¢å
ç½®çä¸ä¸ªdispatchActionçå½æ°ï¼å
·ä½éé¢ä¼°è®¡æ¯å¹²åçä¸å¤ªæ¸
æ¥ï¼ç¨ååæ103 return [hook.memoizedState, dispatch];104}105/**106 * è¿éåæ以ä¸çmountStateåå æ¥è¿è¡107 * 1ãmountWorkInProgressHookè¿ä¸ªå½æ°éé¢è¿åçæ¯ä»ä¹ä¸è¥¿ï¼éé¢æä»ä¹ä¸è¥¿ï¼ä¸ºä»ä¹å½å为hook?108 * 2ãdispatchActionå½æ°109 * 3ãæ¤æ¥å¯ä¸ç¨è¿ä¹è¯¦ç»è¿è¡åæï¼basicStateReduceræ¯ä»ä¹ï¼å®æä»ä¹ç¨ï¼æä¹ç¨ï¼ä»¥åcurrentlyRenderingFiberæ¯ä¸ªä»ä¹ä¸è¥¿110 */111 // 1ãmountWorkInProgressHookæ¯ä¸ªä»ä¹ä¸è¥¿112 function mountWorkInProgressHook(): Hook {113 // å
¶å®å°±æ¯è¿åä¸ä¸ªhookï¼å°±æ¯ä¸ä¸ªç®åç对象114 const hook: Hook = {115 memoizedState: null,116 baseState: null,117 queue: null,118 baseUpdate: null,119 next: null,120 };121 // è¿éå
¶å®å°±æ¯æ建ä¸ä¸ªç±»ä¼¼é¾è¡¨çä¸è¥¿ï¼122 // HookA -> HookB -> HookC -> HookD ?123 // æè§å°±æ¯è¿ä¸ªä¸è¥¿äºæåæå½åé¾è¡¨èç¹è¿å124 if (workInProgressHook === null) {125 // This is the first hook in the list126 // è¿ä¸ªfirstWorkInprogressHookä¼¼ä¹æ¯å
¨å±åé127 firstWorkInProgressHook = workInProgressHook = hook;128 } else {129 // Append to the end of the list130 workInProgressHook = workInProgressHook.next = hook;131 }132 return workInProgressHook;133}134// 2ãæç½äºhookæ¯ä¸ªä»ä¹ä¸è¥¿ï¼ç°å¨ç¬¬äºæ¥ï¼dispatcherActionæ¯ä¸ªä»ä¹ä¸è¥¿?135// è¿ä¸ªä¸è¥¿æ¯è¾é¾åçèµ·æ¥å°±æºå¤æçï¼ä½æ¯ç°å¨å±ä»¬å
ä¸å»å¨å®ï¼å 为å±ä»¬åªæ¯è¿è¡çä¸ä¸ªæè½½å¨ä½ï¼æ²¡æä»»ä½å¨ä½è¡ä¸ºï¼æ æ³è§¦å该å½æ°æ§è¡136function dispatchAction<S, A>(137 fiber: Fiber, // currentlyRenderingFiber138 queue: UpdateQueue<S, A>, // mounté¶æ®µqueue {last: null, lastRenderedReducer: basicStateReducer, lastRenderedState: (initialState: any)}139 action: A, // mounté¶æ®µ action 为undefined140) {141 invariant(142 numberOfReRenders < RE_RENDER_LIMIT,143 'Too many re-renders. React limits the number of renders to prevent ' +144 'an infinite loop.',145 );146 if (__DEV__) {147 warning(148 arguments.length <= 3,149 "State updates from the useState() and useReducer() Hooks don't support the " +150 'second callback argument. To execute a side effect after ' +151 'rendering, declare it in the component body with useEffect().',152 );153 }154 const alternate = fiber.alternate;155 if (156 fiber === currentlyRenderingFiber ||157 (alternate !== null && alternate === currentlyRenderingFiber)158 ) {159 // This is a render phase update. Stash it in a lazily-created map of160 // queue -> linked list of updates. After this render pass, we'll restart161 // and apply the stashed updates on top of the work-in-progress hook.162 didScheduleRenderPhaseUpdate = true;163 const update: Update<S, A> = {164 expirationTime: renderExpirationTime,165 action,166 eagerReducer: null,167 eagerState: null,168 next: null,169 };170 if (renderPhaseUpdates === null) {171 renderPhaseUpdates = new Map();172 }173 const firstRenderPhaseUpdate = renderPhaseUpdates.get(queue);174 if (firstRenderPhaseUpdate === undefined) {175 renderPhaseUpdates.set(queue, update);176 } else {177 // Append the update to the end of the list.178 let lastRenderPhaseUpdate = firstRenderPhaseUpdate;179 while (lastRenderPhaseUpdate.next !== null) {180 lastRenderPhaseUpdate = lastRenderPhaseUpdate.next;181 }182 lastRenderPhaseUpdate.next = update;183 }184 } else {185 flushPassiveEffects();186 const currentTime = requestCurrentTime();187 const expirationTime = computeExpirationForFiber(currentTime, fiber);188 const update: Update<S, A> = {189 expirationTime,190 action,191 eagerReducer: null,192 eagerState: null,193 next: null,194 };195 // Append the update to the end of the list.196 const last = queue.last;197 if (last === null) {198 // This is the first update. Create a circular list.199 update.next = update;200 } else {201 const first = last.next;202 if (first !== null) {203 // Still circular.204 update.next = first;205 }206 last.next = update;207 }208 queue.last = update;209 if (210 fiber.expirationTime === NoWork &&211 (alternate === null || alternate.expirationTime === NoWork)212 ) {213 // The queue is currently empty, which means we can eagerly compute the214 // next state before entering the render phase. If the new state is the215 // same as the current state, we may be able to bail out entirely.216 const lastRenderedReducer = queue.lastRenderedReducer;217 if (lastRenderedReducer !== null) {218 let prevDispatcher;219 if (__DEV__) {220 prevDispatcher = ReactCurrentDispatcher.current;221 ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnUpdateInDEV;222 }223 try {224 const currentState: S = (queue.lastRenderedState: any);225 const eagerState = lastRenderedReducer(currentState, action);226 // Stash the eagerly computed state, and the reducer used to compute227 // it, on the update object. If the reducer hasn't changed by the228 // time we enter the render phase, then the eager state can be used229 // without calling the reducer again.230 update.eagerReducer = lastRenderedReducer;231 update.eagerState = eagerState;232 if (is(eagerState, currentState)) {233 // Fast path. We can bail out without scheduling React to re-render.234 // It's still possible that we'll need to rebase this update later,235 // if the component re-renders for a different reason and by that236 // time the reducer has changed.237 return;238 }239 } catch (error) {240 // Suppress the error. It will throw again in the render phase.241 } finally {242 if (__DEV__) {243 ReactCurrentDispatcher.current = prevDispatcher;244 }245 }246 }247 }248 if (__DEV__) {249 // jest isn't a 'global', it's just exposed to tests via a wrapped function250 // further, this isn't a test file, so flow doesn't recognize the symbol. So...251 // $FlowExpectedError - because requirements don't give a damn about your type sigs.252 if ('undefined' !== typeof jest) {253 warnIfNotCurrentlyActingUpdatesInDev(fiber);254 }255 }256 scheduleWork(fiber, expirationTime);257 }258}259// 第ä¸æ¥ï¼ææ 260// ç°å¨ç¸å½äºåå§å好äºsetStateçhookå°±å¦åæ§è¡äºä¸æ¬¡ä¸é¢ç代ç 261const [name, setName] = useState('initialName');...
index.js
Source:index.js
...36 23 | var subscription = contextValue.subscription;37View compiled38mountMemo39node_modules/react-dom/cjs/react-dom.development.js:1584640 15843 | function mountMemo(nextCreate, deps) {41 15844 | var hook = mountWorkInProgressHook();42 15845 | var nextDeps = deps === undefined ? null : deps;43> 15846 | var nextValue = nextCreate();44 15847 | hook.memoizedState = [nextValue, nextDeps];45 15848 | return nextValue;46 15849 | }47View compiled48useMemo49node_modules/react-dom/cjs/react-dom.development.js:1621950 16216 | ReactCurrentDispatcher$1.current = InvalidNestedHooksDispatcherOnMountInDEV;51 16217 | 52 16218 | try {53> 16219 | return mountMemo(create, deps);54 | ^ 16220 | } finally {55 16221 | ReactCurrentDispatcher$1.current = prevDispatcher;56 16222 | }57View compiled58useMemo59node_modules/react/cjs/react.development.js:153260 1529 | }61 1530 | function useMemo(create, deps) {62 1531 | var dispatcher = resolveDispatcher();63> 1532 | return dispatcher.useMemo(create, deps);64 1533 | }65 1534 | function useImperativeHandle(ref, create, deps) {66 1535 | var dispatcher = resolveDispatcher();67View compiled...
react-dom.js
Source:react-dom.js
...54 return mountProvider(vdom);55 } else if (type && type.$$typeof === REACT_CONTEXT) {56 return mountContext(vdom);57 } else if (type && type.$$typeof === REACT_MEMO) {58 return mountMemo(vdom);59 } else {60 dom = document.createElement(type);// div span p61 }62 // å¤çå±æ§63 if (props) {64 updateProps(dom, {}, props); // æ´æ°domå±æ§65 if (props.children) { // æåéå°±æè½½èªå·±ä¸é¢66 const children = props.children;67 if (typeof children === 'object' && children.type) { // 说æè¿æ¯ä¸ä¸ªReactå
ç´ 68 children._mountIndex = 0; // diffä¸åä¸æ ç¨69 render(children, dom);70 } else if (Array.isArray(children)) { // éå就循ç¯æè½½71 reconcileChildren(props.children, dom);72 }...
handleComponent.js
Source:handleComponent.js
...35 vdom.oldRenderVdom = renderVdom; // è®°å½ådom36 return createDOM(renderVdom); // å½æ°ç»ä»¶è¿åçå°±æ¯vdomï¼åçæçå®dom37}38// å¤çmemoå½æ°ç»ä»¶39export function mountMemo(vdom) {40 // æ£å¸¸æ¸²æ就好41 let {type, props} = vdom;42 let renderVdom = type.type(props);43 vdom.prevProps = props;44 vdom.oldRenderVdom = renderVdom;45 return createDOM(renderVdom);46}47/**48 * 渲æProviderç»ä»¶49 * 1.çæ£è¦æ¸²æçæ¯å®çå¿åchildren50 * 2.æProviderç»ä»¶èªå·±æ¶å°çvalueå±æ§èµå¼ç»context._currentValue51 */52// æä¾è
53export function mountProvider(vdom) {...
useCallback&useMemo.js
Source:useCallback&useMemo.js
...8 const nextDeps = deps === undefined ? null : deps;9 hook.memoizedState = [callback, nextDeps];10 return callback;11}12function mountMemo(nextCreate, deps) {13 // åå
¶ä»hookç¸åï¼é¦å
æ¾å°æ£å¨æ§è¡çhookã14 const hook = mountWorkInProgressHook();15 const nextDeps = deps === undefined ? null : deps;16 // ææ们çä¾èµé¡¹ä¿åå¨hookä¸å¹¶è¿åçcallbackï¼å¹¶æ§è¡ã17 const nextValue = nextCreate();18 hook.memoizedState = [nextValue, nextDeps];19 return nextValue;20}21function updateCallback(callback, deps) {22 const hook = updateWorkInProgressHook();23 const nextDeps = deps === undefined ? null : deps;24 const prevState = hook.memoizedState;25 if (prevState !== null) {26 if (nextDeps !== null) {...
use-component.js
Source:use-component.js
1import { useMemo } from 'react';2/* eslint-disable no-continue */3export function useComponent() {4 // TODO: Detect DEBUG mode5 return useMemo(() => {6 try {7 throw new Error('fake error');8 } catch (ex) {9 const { stack } = ex;10 const stacks = stack.split('\n');11 for (let i = 0; i < stacks.length; i += 1) {12 if (i === 0) {13 // first line will be 'Error: fake error'14 continue;15 }16 // a typical stack line looks like this:17 // "at useComponent (webpack://1:2:3)"18 const stackSegs = stacks[i].trim().split(' ');19 // we only need the 2nd segment, which is the function name20 const funcName = stackSegs[1] || '';21 // we'll ignore anything that is a React hook22 if (!funcName23 || funcName === 'eval'24 || funcName === 'mountMemo'25 || funcName === 'Object.useMemo'26 || funcName.startsWith('use')) {27 continue;28 }29 // we'll return on the first non-hook function name, which is most likely30 // to be our calling component name;31 return funcName;32 }33 }34 return '__noname__';35 }, []);...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 const url = await page.url();7 console.log(url);8 await page.close();9 await context.close();10 await browser.close();11})();
Using AI Code Generation
1const { chromium } = require("playwright");2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.screenshot({ path: "google.png" });7 await page.close();8 await context.close();9 await browser.close();10})();11const { chromium } = require("playwright");12(async () => {13 const browser = await chromium.launch();14 const context = await browser.newContext();15 const page = await context.newPage();16 await page.screenshot({ path: "google.png" });17 await page.close();18 await context.close();19 await browser.close();20})();21const { chromium } = require("playwright");22(async () => {23 const browser = await chromium.launch();24 const context = await browser.newContext();25 const page = await context.newPage();26 await page.screenshot({ path: "google.png" });27 await page.close();28 await context.close();29 await browser.close();30})();31const { chromium } = require("playwright");32(async () => {33 const browser = await chromium.launch();34 const context = await browser.newContext();35 const page = await context.newPage();36 await page.screenshot({ path: "google.png" });37 await page.close();38 await context.close();39 await browser.close();40})();41const { chromium } = require("playwright");42(async () => {43 const browser = await chromium.launch();44 const context = await browser.newContext();45 const page = await context.newPage();46 await page.screenshot({ path: "google.png" });47 await page.close();48 await context.close();49 await browser.close();50})();51const {
Using AI Code Generation
1const { mountMemo } = require('playwright/lib/server/memo');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const memo = mountMemo(page);8 await page.screenshot({ path: 'example.png' });9 await browser.close();10})();11const { mountMemo } = require('playwright/lib/server/memo');12const { chromium } = require('playwright');13(async () => {14 const browser = await chromium.launch();15 const context = await browser.newContext();16 const page = await context.newPage();17 const memo = mountMemo(page);18 await page.screenshot({ path: 'example.png' });19 await browser.close();20})();21const { mountMemo } = require('playwright/lib/server/memo');22const { chromium } = require('playwright');23(async () => {24 const browser = await chromium.launch();25 const context = await browser.newContext();26 const page = await context.newPage();27 const memo = mountMemo(page);28 await page.screenshot({ path: 'example.png' });29 await browser.close();30})();31const { mountMemo } = require('playwright/lib/server/memo');32const { chromium } = require('playwright');33(async () => {34 const browser = await chromium.launch();35 const context = await browser.newContext();36 const page = await context.newPage();37 const memo = mountMemo(page);38 await page.screenshot({ path: 'example.png' });39 await browser.close();40})();41const { mountMemo } = require('playwright/lib/server/memo');42const { chromium } = require('playwright');43(async () => {44 const browser = await chromium.launch();45 const context = await browser.newContext();
Using AI Code Generation
1const { mountMemo } = require('playwright/lib/server/browserContext');2const { chromium } = require('playwright');3const browser = await chromium.launch();4const context = await browser.newContext();5const page = await context.newPage();6const memo = await mountMemo(page, 'test');7console.log(memo);8const { unmountMemo } = require('playwright/lib/server/browserContext');9const { chromium } = require('playwright');10const browser = await chromium.launch();11const context = await browser.newContext();12const page = await context.newPage();13const memo = await unmountMemo(page, 'test');14console.log(memo);15const { getMemo } = require('playwright/lib/server/browserContext');16const { chromium } = require('playwright');17const browser = await chromium.launch();18const context = await browser.newContext();19const page = await context.newPage();20const memo = await getMemo(page, 'test');21console.log(memo);22{ memo: 'test' }
Using AI Code Generation
1const { mountMemo } = require('playwright-core/lib/server/browserContext');2const { Page } = require('playwright-core/lib/server/page');3const { Frame } = require('playwright-core/lib/server/frames');4const { ElementHandle } = require('playwright-core/lib/server/dom');5const page = new Page(null, null, null, null, null, null, null, null);6const frame = new Frame(page, null, null, null, null, null, null, null, null, null, null, null, null);7const elementHandle = new ElementHandle(frame, null, null, null, null, null, null, null);8const memo = { a: 1, b: 2 };9mountMemo(elementHandle, memo);10console.log(elementHandle._memo);11console.log(frame._memo);12console.log(page._memo);13console.log(page._browserContext._memo);14console.log(page._browserContext._browser._memo);15console.log(page._browserContext._browser._server._memo);16console.log(page._browserContext._browser._server._playwright._memo);17console.log(page._browserContext._browser._server._playwright._playwrightCore._memo);18console.log(page._browserContext._browser._server._playwright._playwrightCore._playwright._memo);19console.log(page._browserContext._browser._server._playwright._playwrightCore._playwright._playwrightCore._memo);20console.log(page._browserContext._browser._server._playwright._playwrightCore._playwright._playwrightCore._playwright._memo);
Using AI Code Generation
1const { mountMemo } = require('playwright/lib/server/browserContext');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const page2 = await memo.page();8 const context2 = await memo.context();9 const browser2 = await memo.browser();10 await browser.close();11})();
Using AI Code Generation
1const { mountMemo } = require('playwright/lib/server/frames');2mountMemo('my-memo', myMemo);3const { test } = require('@playwright/test');4test('My test', async ({ page }) => {5 await page.setContent('<my-memo></my-memo>');6});7class MyMemo extends HTMLElement {8 connectedCallback() {9 this.innerHTML = 'Hello World';10 }11}12customElements.define('my-memo', MyMemo);
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!