blog's People
blog's Issues
js(正负)前瞻和(正负)后顾
位运算在react源码中的运用
在react源码中经常可以看到一些运算
workInProgress.effectTag |= PerformedWork
workInProgress.effectTag |= PerformedWork;
workInProgress.effectTag |= Update;
workInProgress.effectTag |= Hydrating;
if ((finishedWork.effectTag & Passive) !== NoEffect) {
// pass
}
...
通过查找源码shared/ReactSideEffectTags.js得知effectTag具体会有如下值
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
export type SideEffectTag = number;
// Don't change these two values. They're used by React Dev Tools.
export const NoEffect = /* */ 0b0000000000000;
export const PerformedWork = /* */ 0b0000000000001;
// You can change the rest (and add more).
export const Placement = /* */ 0b0000000000010;
export const Update = /* */ 0b0000000000100;
export const PlacementAndUpdate = /* */ 0b0000000000110;
export const Deletion = /* */ 0b0000000001000;
export const ContentReset = /* */ 0b0000000010000;
export const Callback = /* */ 0b0000000100000;
export const DidCapture = /* */ 0b0000001000000;
export const Ref = /* */ 0b0000010000000;
export const Snapshot = /* */ 0b0000100000000;
export const Passive = /* */ 0b0001000000000;
export const Hydrating = /* */ 0b0010000000000;
export const HydratingAndUpdate = /* */ 0b0010000000100;
// Passive & Update & Callback & Ref & Snapshot
export const LifecycleEffectMask = /* */ 0b0001110100100;
// Union of all host effects
export const HostEffectMask = /* */ 0b0011111111111;
export const Incomplete = /* */ 0b0100000000000;
export const ShouldCapture = /* */ 0b1000000000000;
可以看到大部分的值都只有一位是1,其他位都是0
假设当前的effectTag为Update,
// Don't change these two values. They're used by React Dev Tools.
var NoEffect = /* */ 0b0000000000000;
var PerformedWork = /* */ 0b0000000000001;
// You can change the rest (and add more).
var Placement = /* */ 0b0000000000010;
var Update = /* */ 0b0000000000100;
var PlacementAndUpdate = /* */ 0b0000000000110;
var Deletion = /* */ 0b0000000001000;
var ContentReset = /* */ 0b0000000010000;
var Callback = /* */ 0b0000000100000;
var DidCapture = /* */ 0b0000001000000;
var Ref = /* */ 0b0000010000000;
var Snapshot = /* */ 0b0000100000000;
var Passive = /* */ 0b0001000000000;
var Hydrating = /* */ 0b0010000000000;
var HydratingAndUpdate = /* */ 0b0010000000100;
// Passive & Update & Callback & Ref & Snapshot
var LifecycleEffectMask = /* */ 0b0001110100100;
// Union of all host effects
var HostEffectMask = /* */ 0b0011111111111;
var Incomplete = /* */ 0b0100000000000;
var ShouldCapture = /* */ 0b1000000000000;
var effectTag = Update
effectTag |= Ref // effectTag此时为0b0000010000100
console.log((effectTag & Update) === Update) // true
console.log((effectTag & Ref) === Ref) // true
console.log((effectTag & Placement) === Placement) // false
effectTag &= ~Ref
console.log((effectTag & Ref) === Ref) // false
&操作是用来判断在某个变量中是否含有某个属性的
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.