-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
900 lines (509 loc) · 92.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>社会小安哥</title>
<!-- keywords -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta property="og:type" content="website">
<meta property="og:title" content="社会小安哥">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="社会小安哥">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="纪安">
<meta name="twitter:card" content="summary">
<link rel="alternative" href="/atom.xml" title="社会小安哥" type="application/atom+xml">
<link rel="icon" href="http://7xkj1z.com1.z0.glb.clouddn.com/head.jpg">
<link rel="stylesheet" href="/css/style.css">
<script src="//cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<meta name="generator" content="Hexo 4.2.1"></head>
<body>
<div id="container">
<div id="particles-js"></div>
<div class="left-col">
<div class="overlay"></div>
<div class="intrude-less">
<header id="header" class="inner">
<a href="/" class="profilepic">
<img lazy-src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2336376645,1481998160&fm=26&gp=0.jpg" class="js-avatar">
</a>
<hgroup>
<h1 class="header-author"><a href="/">纪安</a></h1>
</hgroup>
<div class="switch-btn">
<div class="icon">
<div class="icon-ctn">
<div class="icon-wrap icon-house" data-idx="0">
<div class="birdhouse"></div>
<div class="birdhouse_holes"></div>
</div>
<div class="icon-wrap icon-ribbon hide" data-idx="1">
<div class="ribbon"></div>
</div>
<div class="icon-wrap icon-link hide" data-idx="2">
<div class="loopback_l"></div>
<div class="loopback_r"></div>
</div>
</div>
</div>
<div class="tips-box hide">
<div class="tips-arrow"></div>
<ul class="tips-inner">
<li>菜单</li>
<li>标签</li>
<li>友情链接</li>
</ul>
</div>
</div>
<div class="switch-area">
<div class="switch-wrap">
<section class="switch-part switch-part1">
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives">所有文章</a></li>
</ul>
</nav>
<nav class="header-nav">
<div class="social">
</div>
</nav>
</section>
<section class="switch-part switch-part2">
<div class="widget tagcloud" id="js-tagcloud">
</div>
</section>
<section class="switch-part switch-part3">
<div id="js-friends">
<a target="_blank" class="main-nav-link switch-friends-link" href="https://github.com/smackgg/hexo-theme-smackdown">smackdown</a>
</div>
</section>
</div>
</div>
</header>
</div>
</div>
<div class="mid-col">
<nav id="mobile-nav">
<div class="overlay">
<div class="slider-trigger"></div>
<h1 class="header-author js-mobile-header hide">纪安</h1>
</div>
<div class="intrude-less">
<header id="header" class="inner">
<div class="profilepic">
<img lazy-src="https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2336376645,1481998160&fm=26&gp=0.jpg" class="js-avatar">
</div>
<hgroup>
<h1 class="header-author">纪安</h1>
</hgroup>
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives">所有文章</a></li>
<div class="clearfix"></div>
</ul>
</nav>
<nav class="header-nav">
<div class="social">
</div>
</nav>
</header>
</div>
</nav>
<div class="body-wrap">
<article id="post-input输入框输入时过滤空格某些输入法无法键入解决办法" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2021/03/24/input%E8%BE%93%E5%85%A5%E6%A1%86%E8%BE%93%E5%85%A5%E6%97%B6%E8%BF%87%E6%BB%A4%E7%A9%BA%E6%A0%BC%E6%9F%90%E4%BA%9B%E8%BE%93%E5%85%A5%E6%B3%95%E6%97%A0%E6%B3%95%E9%94%AE%E5%85%A5%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/" class="article-date">
<time datetime="2021-03-24T14:46:03.841Z" itemprop="datePublished">2021-03-24</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2021/03/24/input%E8%BE%93%E5%85%A5%E6%A1%86%E8%BE%93%E5%85%A5%E6%97%B6%E8%BF%87%E6%BB%A4%E7%A9%BA%E6%A0%BC%E6%9F%90%E4%BA%9B%E8%BE%93%E5%85%A5%E6%B3%95%E6%97%A0%E6%B3%95%E9%94%AE%E5%85%A5%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/">
input输入框输入时过滤空格某些输入法无法键入解决办法
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="input输入框输入时过滤空格某些输入法无法键入解决办法"><a href="#input输入框输入时过滤空格某些输入法无法键入解决办法" class="headerlink" title="input输入框输入时过滤空格某些输入法无法键入解决办法"></a>input输入框输入时过滤空格某些输入法无法键入解决办法</h3><h4 id="产生背景"><a href="#产生背景" class="headerlink" title="产生背景"></a>产生背景</h4><blockquote>
<p>需求中需要再输入时过滤空格及其特殊字符,由于不同输入法的<code>键入方式不一致</code>,有些输入法会键入一个空格在多个字符键入的时候,从而导致文字无法正常键入</p>
</blockquote>
<h4 id="解决过程"><a href="#解决过程" class="headerlink" title="解决过程"></a>解决过程</h4><ol>
<li><blockquote>
<p>用 compositionstart 和 compositionend这两个钩子,我们平时没有用过,大部分浏览器执行顺序是,<strong>compositionstart————compositionend————input监听事件</strong>,查阅资料可知,input事件的event中的<strong>isComposing</strong>代表是否正在输入,获取方式为:<code>event.nativeEvent.isComposing</code></p>
</blockquote>
</li>
<li><blockquote>
<p>这样就解决了大部分浏览器的问题,但是唯独谷歌对于这几个钩子执行顺序不一样,<strong>compositionstart————input监听事件————compositionend</strong>,看起来谷歌执行顺序看起来符合钩子的定义。我们就需要在<strong>compositionend</strong>钩子里去做我们的去空格操作</p>
</blockquote>
</li>
<li><blockquote>
<p>为了做兼容,我们需要判断当前浏览器是不是谷歌,或者说是谷歌内核,我们利用<br><a href="https://www.w3school.com.cn/jsref/dom_obj_navigator.asp" target="_blank" rel="noopener">Navigator</a>,其中的<code>navigator.userAgent</code>,利用window.chrome和’/Chrome/‘正则去校验是否为谷歌浏览器</p>
</blockquote>
</li>
</ol>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-react的context学习" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2021/03/24/react%E7%9A%84context%E5%AD%A6%E4%B9%A0/" class="article-date">
<time datetime="2021-03-24T14:46:00.342Z" itemprop="datePublished">2021-03-24</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2021/03/24/react%E7%9A%84context%E5%AD%A6%E4%B9%A0/">
react的context学习
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="react的context学习"><a href="#react的context学习" class="headerlink" title="react的context学习"></a>react的context学习</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> logo <span class="keyword">from</span> <span class="string">'./logo.svg'</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">'./App.css'</span>;</span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 小明的爷爷想通过小明的爸爸给小明100元钱</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Grandpa</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="xml"><span class="tag"><<span class="name">Father</span> <span class="attr">money</span>=<span class="string">{100}</span>></span>爷爷给爸爸:100元<span class="tag"></<span class="name">Father</span>></span></span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Father</span>(<span class="params">props</span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="xml"><span class="tag"><<span class="name">XiaoMing</span> <span class="attr">money</span>=<span class="string">{props.money-50}</span>></span></span></span><br><span class="line"><span class="xml"> {props.children}小明爸爸扣了50元<span class="tag"></<span class="name">XiaoMing</span>></span></span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">XiaoMing</span>(<span class="params">props</span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="xml"><span class="tag"><<span class="name">div</span>></span>{props.children}小明只剩下50元<span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line">}</span><br><span class="line"><span class="comment">// 由于层层传递甚至出现中饱私囊的现象,于是我们使用 context</span></span><br><span class="line"><span class="comment">// Context 可以让我们无须明确地传遍每一个组件,就能将值深入传递进组件树。</span></span><br><span class="line"><span class="keyword">const</span> ThemeContext = React.createContext();</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Grandpa1</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <ThemeContext.Provider value={<span class="number">100</span>}></span><br><span class="line"> <Father1/></span><br><span class="line"> <<span class="regexp">/ThemeContext.Provider></span></span><br><span class="line"><span class="regexp"> )</span></span><br><span class="line"><span class="regexp">}</span></span><br><span class="line"><span class="regexp">function Father1(){</span></span><br><span class="line"><span class="regexp"> return <XiaoMing1 /</span>></span><br><span class="line">}</span><br><span class="line"><span class="comment">// 指定 contextType 读取当前的 theme context。</span></span><br><span class="line"> <span class="comment">// React 会往上找到最近的 theme Provider,然后使用它的值。</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">XiaoMing1</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>{</span><br><span class="line"> <span class="keyword">static</span> contextType = ThemeContext;</span><br><span class="line"> render() {</span><br><span class="line"> <span class="keyword">return</span> <span class="xml"><span class="tag"><<span class="name">button</span>></span>{this.context}<span class="tag"><<span class="name">XiaoMing1Son</span>/></span><span class="tag"></<span class="name">button</span>></span></span></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 小明的儿子也可以拿到</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">XiaoMing1Son</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>{</span><br><span class="line"> <span class="keyword">static</span> contextType = ThemeContext;</span><br><span class="line"> render() {</span><br><span class="line"> <span class="keyword">return</span> <span class="xml"><span class="tag"><<span class="name">button</span>></span>{this.context}<span class="tag"></<span class="name">button</span>></span></span></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 这样就做到了组件的深层次的传递,也不用层层写</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Grandpa1;</span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-react函数式组件和类组件创建ref的方法" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2021/01/19/react%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6%E5%92%8C%E7%B1%BB%E7%BB%84%E4%BB%B6%E5%88%9B%E5%BB%BAref%E7%9A%84%E6%96%B9%E6%B3%95/" class="article-date">
<time datetime="2021-01-19T13:35:11.229Z" itemprop="datePublished">2021-01-19</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2021/01/19/react%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6%E5%92%8C%E7%B1%BB%E7%BB%84%E4%BB%B6%E5%88%9B%E5%BB%BAref%E7%9A%84%E6%96%B9%E6%B3%95/">
react函数式组件和类组件创建ref的方法
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="react函数式组件和类组件创建ref的方法"><a href="#react函数式组件和类组件创建ref的方法" class="headerlink" title="react函数式组件和类组件创建ref的方法"></a>react函数式组件和类组件创建ref的方法</h3><h4 id="类组件创建ref"><a href="#类组件创建ref" class="headerlink" title="类组件创建ref"></a>类组件创建ref</h4><blockquote>
<p>父组件</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引入React</span></span><br><span class="line"><span class="keyword">import</span> React, { Component } <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> EditableTable <span class="keyword">from</span> <span class="string">'./EditableTable'</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Content</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>{</span><br><span class="line"> <span class="keyword">constructor</span>(props) {</span><br><span class="line"> <span class="keyword">super</span>(props);</span><br><span class="line"> <span class="comment">// 通过React.createRef()创建ref,挂载到父组件上</span></span><br><span class="line"> <span class="keyword">this</span>.editTableEl = React.createRef();</span><br><span class="line"> }</span><br><span class="line"> render() {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <div></span><br><span class="line"> <EditableTable</span><br><span class="line"> <span class="comment">// 挂载ref</span></span><br><span class="line"> ref={<span class="keyword">this</span>.editTableEl}</span><br><span class="line"> /></span><br><span class="line"> <button onClick={()=>{alert(<span class="keyword">this</span>.editTableEl.current.os())}}>父组件的button<<span class="regexp">/button></span></span><br><span class="line"><span class="regexp"> </</span>div></span><br><span class="line"> )</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<blockquote>
<p>子组件</p>
</blockquote>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引入React</span></span><br><span class="line"><span class="keyword">import</span> <span class="type">React</span>, { <span class="type">Component</span> } from <span class="symbol">'reac</span>t';</span><br><span class="line">export <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">EditableTable</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>{</span><br><span class="line"> constructor(props) {</span><br><span class="line"> <span class="keyword">super</span>(props);</span><br><span class="line"> <span class="keyword">this</span>.state={</span><br><span class="line"> inp:<span class="string">""</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 通过React.createRef()创建ref,挂载到组件上</span></span><br><span class="line"> <span class="keyword">this</span>.editTableEls = <span class="type">React</span>.createRef();</span><br><span class="line"> }</span><br><span class="line"> componentDidMount(){</span><br><span class="line"> <span class="keyword">this</span>.editTableEls.current.os=<span class="keyword">this</span>.os</span><br><span class="line"> }</span><br><span class="line"> os=()=><span class="keyword">this</span>.state.inp</span><br><span class="line"> render() {</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <div>子组件的输入框</span><br><span class="line"> <input <span class="class"><span class="keyword">type</span></span>=<span class="string">"text"</span> ref={<span class="keyword">this</span>.editTableEls} value={<span class="keyword">this</span>.state.inp} onChange={(e)=><span class="keyword">this</span>.setState({inp:e.target.value})}/></span><br><span class="line"> </div></span><br><span class="line"> )</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="函数式组件创建ref"><a href="#函数式组件创建ref" class="headerlink" title="函数式组件创建ref"></a>函数式组件创建ref</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React, { useRef, useImperativeHandle } <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">'react-dom'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//父组件</span></span><br><span class="line"><span class="keyword">const</span> App = <span class="function"><span class="params">props</span> =></span> {</span><br><span class="line"> <span class="keyword">const</span> fancyInputRef = useRef();</span><br><span class="line"> <span class="comment">// 创建ref相当于React.createRef()</span></span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <div></span><br><span class="line"> <FancyInput ref={fancyInputRef} /></span><br><span class="line"> {<span class="comment">/* 挂载ref */</span>}</span><br><span class="line"> <button</span><br><span class="line"> onClick={() => fancyInputRef.current.focus()}</span><br><span class="line"> >父组件调用子组件的 focus<<span class="regexp">/button></span></span><br><span class="line"><span class="regexp"> </</span>div></span><br><span class="line"> )</span><br><span class="line">}</span><br><span class="line"><span class="comment">// 子组件</span></span><br><span class="line"><span class="keyword">const</span> FancyInput = React.forwardRef(<span class="function">(<span class="params">props, ref</span>) =></span> {</span><br><span class="line"> <span class="keyword">const</span> inputRef = useRef();</span><br><span class="line"> useImperativeHandle(ref, () => ({</span><br><span class="line"> focus: <span class="function"><span class="params">()</span> =></span> {</span><br><span class="line"> inputRef.current.focus();</span><br><span class="line"> }</span><br><span class="line"> }));</span><br><span class="line"> <span class="comment">// useImperativeHandle()第一个参数是cueent对象的ref,第二个是个函数,返回一个对象,第三个参数,只有参数里值发生变化父组件接收的值才会变化才会引发变化</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="xml"><span class="tag"><<span class="name">input</span> <span class="attr">ref</span>=<span class="string">{inputRef}</span> <span class="attr">type</span>=<span class="string">"text"</span> /></span></span></span><br><span class="line">});</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App</span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-react 内存泄露常见问题解决方案" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/12/30/react%20%E5%86%85%E5%AD%98%E6%B3%84%E9%9C%B2%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" class="article-date">
<time datetime="2020-12-29T17:08:34.398Z" itemprop="datePublished">2020-12-30</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/12/30/react%20%E5%86%85%E5%AD%98%E6%B3%84%E9%9C%B2%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/">
react 内存泄露常见问题解决方案
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="react-内存泄露常见问题解决方案"><a href="#react-内存泄露常见问题解决方案" class="headerlink" title="react 内存泄露常见问题解决方案"></a>react 内存泄露常见问题解决方案</h3><h4 id="报错分析"><a href="#报错分析" class="headerlink" title="报错分析"></a>报错分析</h4><figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Cant <span class="keyword">perform</span> a React state <span class="keyword">update</span> <span class="keyword">on</span> an unmounted component. This <span class="keyword">is</span> a <span class="keyword">no</span>-op, but it indicates a memory leak <span class="keyword">in</span> your application. <span class="keyword">To</span> fix, cancel <span class="keyword">all</span> subscriptions <span class="keyword">and</span> asynchronous tasks <span class="keyword">in</span> the componentWillUnmount <span class="keyword">method</span>”</span><br></pre></td></tr></table></figure>
<h4 id="什么是内存泄露"><a href="#什么是内存泄露" class="headerlink" title="什么是内存泄露?"></a>什么是内存泄露?</h4><ul>
<li>不再用到的内存,没有及时释放,就叫做内存泄漏(memory leak)。<h4 id="几种常见的内存泄漏"><a href="#几种常见的内存泄漏" class="headerlink" title="几种常见的内存泄漏"></a>几种常见的内存泄漏</h4></li>
<li>全局变量引起的内存泄漏</li>
<li>闭包引起的内存泄漏</li>
<li>dom清空或删除时,事件未清除导致的内存泄漏<h4 id="例如"><a href="#例如" class="headerlink" title="例如"></a>例如</h4></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">componentWillMount: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> <span class="keyword">var</span> onLogin = <span class="keyword">this</span>.props.onLogin || <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{},</span><br><span class="line"> onLogout = <span class="keyword">this</span>.props.onLogout || <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{};</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">this</span>.on(<span class="string">'authChange'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'user authenticated:'</span>, <span class="keyword">this</span>.state.isAuthenticated);</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.state.isAuthenticated</span><br><span class="line"> ? onLogin(<span class="keyword">this</span>.state)</span><br><span class="line"> : onLogout(<span class="keyword">this</span>.state);</span><br><span class="line"> }.bind(<span class="keyword">this</span>));</span><br><span class="line"> },</span><br><span class="line"> 很明显这种情况就是在 dom 结构销毁的时候,事件却没有清除导致的内存泄漏,所以我们需要在componentWillUnmount的时候去清除挂载的方法<span class="comment">//</span></span><br><span class="line"> componentWillUnmount: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> <span class="keyword">this</span>.off(<span class="string">'authChange'</span>, <span class="keyword">this</span>.authChange);</span><br><span class="line"> <span class="keyword">this</span>.authChange = <span class="literal">null</span>;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-条形码的插件生成" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/12/30/%E6%9D%A1%E5%BD%A2%E7%A0%81%E7%9A%84%E6%8F%92%E4%BB%B6%E7%94%9F%E6%88%90/" class="article-date">
<time datetime="2020-12-29T17:08:34.395Z" itemprop="datePublished">2020-12-30</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/12/30/%E6%9D%A1%E5%BD%A2%E7%A0%81%E7%9A%84%E6%8F%92%E4%BB%B6%E7%94%9F%E6%88%90/">
条形码的插件生成
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="React-生成二维码-条形码的插件"><a href="#React-生成二维码-条形码的插件" class="headerlink" title="React 生成二维码/条形码的插件"></a>React 生成二维码/条形码的插件</h3><p>####安装</p>
<blockquote>
<p>npm install qrcode.react –save</p>
</blockquote>
<h4 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h4><blockquote>
<p>import QRCode from ‘qrcode.react’;</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">React.render(</span><br><span class="line"> <QRCode value=<span class="string">"https://jian521ymn.github.io/"</span> />,</span><br><span class="line"> mountNode</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<h4 id="属性及其用法"><a href="#属性及其用法" class="headerlink" title="属性及其用法"></a>属性及其用法</h4><table>
<thead>
<tr>
<th align="left">prop</th>
<th align="right">type</th>
<th align="center">default value</th>
</tr>
</thead>
<tbody><tr>
<td align="left">value</td>
<td align="right">string</td>
<td align="center"></td>
</tr>
<tr>
<td align="left">renderAs</td>
<td align="right">string (‘canvas’ ‘svg’)</td>
<td align="center">‘canvas’</td>
</tr>
<tr>
<td align="left">size</td>
<td align="right">number</td>
<td align="center">128</td>
</tr>
<tr>
<td align="left">bgColor</td>
<td align="right">string (css color)</td>
<td align="center">“#FFFFFF”</td>
</tr>
<tr>
<td align="left">fgColor</td>
<td align="right">string (css color)</td>
<td align="center">“#000000”</td>
</tr>
<tr>
<td align="left">level</td>
<td align="right">string (‘L’ ‘M’ ‘Q’ ‘H’)</td>
<td align="center">‘L’</td>
</tr>
<tr>
<td align="left">includeMargin</td>
<td align="right">boolean</td>
<td align="center">false</td>
</tr>
</tbody></table>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-利用canves画布实现获取指定区域的颜色值" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/12/30/%E5%88%A9%E7%94%A8canves%E7%94%BB%E5%B8%83%E5%AE%9E%E7%8E%B0%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E5%8C%BA%E5%9F%9F%E7%9A%84%E9%A2%9C%E8%89%B2%E5%80%BC/" class="article-date">
<time datetime="2020-12-29T17:08:34.393Z" itemprop="datePublished">2020-12-30</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/12/30/%E5%88%A9%E7%94%A8canves%E7%94%BB%E5%B8%83%E5%AE%9E%E7%8E%B0%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E5%8C%BA%E5%9F%9F%E7%9A%84%E9%A2%9C%E8%89%B2%E5%80%BC/">
利用canves画布实现获取指定区域的颜色值
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="利用canves画布实现获取指定区域的颜色值"><a href="#利用canves画布实现获取指定区域的颜色值" class="headerlink" title="利用canves画布实现获取指定区域的颜色值"></a>利用canves画布实现获取指定区域的颜色值</h3><h4 id="实现原理"><a href="#实现原理" class="headerlink" title="实现原理"></a>实现原理</h4><ul>
<li>利用文件选择器获得图片文件base64数据</li>
<li>利用canves对图片进行重绘</li>
<li>截取canves相对应区域内容</li>
<li>读取该色素点颜色范围</li>
<li>输出<br>####代码</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">HTML</span> <span class="meta-keyword">PUBLIC</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>获取图片像素颜色,利用cancves实现<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>></span></span><br><span class="line"> body {</span><br><span class="line"> margin: 0px;</span><br><span class="line"><span class="css"> <span class="selector-tag">background</span>: <span class="selector-id">#f2f2f0</span>;</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> p {</span><br><span class="line"> margin: 0px;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="css"> <span class="selector-class">.title</span> {</span></span><br><span class="line"><span class="css"> <span class="selector-tag">color</span>: <span class="selector-id">#FFFF00</span>;</span></span><br><span class="line"><span class="css"> <span class="selector-tag">background</span>: <span class="selector-id">#000000</span>;</span></span><br><span class="line"> text-align: center;</span><br><span class="line"> font-size: 24px;</span><br><span class="line"> line-height: 50px;</span><br><span class="line"> font-weight: bold;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="css"> <span class="selector-class">.file</span> {</span></span><br><span class="line"> position: absolute;</span><br><span class="line"> width: 100%;</span><br><span class="line"> font-size: 90px;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="css"> <span class="selector-class">.filebtn</span> {</span></span><br><span class="line"> display: block;</span><br><span class="line"> position: relative;</span><br><span class="line"> height: 110px;</span><br><span class="line"><span class="css"> <span class="selector-tag">color</span>: <span class="selector-id">#FFFFFF</span>;</span></span><br><span class="line"><span class="css"> <span class="selector-tag">background</span>: <span class="selector-id">#06980e</span>;</span></span><br><span class="line"> font-size: 48px;</span><br><span class="line"> line-height: 110px;</span><br><span class="line"> text-align: center;</span><br><span class="line"> cursor: pointer;</span><br><span class="line"><span class="css"> <span class="selector-tag">border</span>: 3<span class="selector-tag">px</span> <span class="selector-tag">solid</span> <span class="selector-id">#cccccc</span>;</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="css"> <span class="selector-class">.filebtn</span><span class="selector-pseudo">:hover</span> {</span></span><br><span class="line"><span class="css"> <span class="selector-tag">background</span>: <span class="selector-id">#04bc0d</span>;</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="css"> <span class="selector-class">.showimg</span> {</span></span><br><span class="line"> margin: 10px auto 10px auto;</span><br><span class="line"><span class="css"> <span class="selector-tag">-webkit-transition</span>: <span class="selector-tag">all</span> <span class="selector-class">.3s</span>;</span></span><br><span class="line"><span class="css"> <span class="selector-tag">transition</span>: <span class="selector-tag">all</span> <span class="selector-class">.3s</span>;</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="css"> <span class="selector-class">.showimg</span> <span class="selector-tag">span</span> {</span></span><br><span class="line"> width: 1px;</span><br><span class="line"> height: 1px;</span><br><span class="line"> display: block;</span><br><span class="line"> margin: -1px 0px 0px -1px;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="css"> <span class="selector-class">.css_code</span> {</span></span><br><span class="line"> margin: 10px;</span><br><span class="line"> padding: 10px;</span><br><span class="line"> display: none;</span><br><span class="line"><span class="css"> <span class="selector-tag">border</span>: 1<span class="selector-tag">px</span> <span class="selector-tag">solid</span> <span class="selector-id">#FFCC00</span>;</span></span><br><span class="line"> font-size: 12px;</span><br><span class="line"><span class="css"> <span class="selector-tag">background</span>: <span class="selector-id">#F1F1F1</span>;</span></span><br><span class="line"> white-space: pre-wrap;</span><br><span class="line"> word-wrap: break-word;</span><br><span class="line"> height: 300px;</span><br><span class="line"> overflow: auto;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="css"> <span class="selector-class">.css_code</span><span class="selector-pseudo">:hover</span> {</span></span><br><span class="line"><span class="css"> <span class="selector-tag">background</span>: <span class="selector-id">#DEFEDE</span>;</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="css"> <span class="selector-id">#box</span> <span class="selector-tag">img</span> {</span></span><br><span class="line"> width: 100px;</span><br><span class="line"> margin: 20px;</span><br><span class="line"> }</span><br><span class="line"> <span class="tag"></<span class="name">style</span>></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"title"</span>></span>获取图片像素颜色,转换<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span><span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"file"</span> <span class="attr">class</span>=<span class="string">"file"</span> <span class="attr">id</span>=<span class="string">"img"</span> <span class="attr">multiple</span>=<span class="string">"multiple"</span>></span><span class="tag"><<span class="name">label</span> <span class="attr">class</span>=<span class="string">"filebtn"</span> <span class="attr">for</span>=<span class="string">"img"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">title</span>=<span class="string">"JPG,GIF,PNG"</span>></span>请选择图片<span class="tag"></<span class="name">label</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"showimg"</span> <span class="attr">id</span>=<span class="string">"showimg"</span>></span><span class="tag"><<span class="name">span</span>></span><span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"css_code"</span> <span class="attr">id</span>=<span class="string">"css_code"</span>></span><span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"box"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>></span></span><br><span class="line"><span class="javascript"> <span class="keyword">let</span> ary = [],</span></span><br><span class="line"> ind = 0,</span><br><span class="line"><span class="javascript"> str = <span class="string">``</span></span></span><br><span class="line"><span class="javascript"> <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span></span><br><span class="line"></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> canvas = <span class="built_in">document</span>.createElement(<span class="string">'canvas'</span>);</span></span><br><span class="line"><span class="actionscript"> <span class="keyword">var</span> context = canvas.getContext(<span class="string">'2d'</span>);</span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> showimg = <span class="built_in">document</span>.getElementById(<span class="string">'showimg'</span>);</span></span><br><span class="line"><span class="actionscript"> <span class="keyword">var</span> shadow = showimg.getElementsByTagName(<span class="string">'span'</span>)[<span class="number">0</span>];</span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> css_code = <span class="built_in">document</span>.getElementById(<span class="string">'css_code'</span>);</span></span><br><span class="line"></span><br><span class="line"><span class="javascript"> <span class="built_in">document</span>.getElementById(<span class="string">'img'</span>).onchange = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">let</span> eve = event.target</span></span><br><span class="line"><span class="javascript"> <span class="keyword">let</span> timer = setInterval(<span class="function"><span class="params">()</span> =></span> {</span></span><br><span class="line"></span><br><span class="line"><span class="actionscript"> <span class="keyword">var</span> img = eve.files[ind];</span></span><br><span class="line"></span><br><span class="line"><span class="actionscript"> <span class="comment">// 检查能否读取图片 </span></span></span><br><span class="line"> if (!img) {</span><br><span class="line"><span class="actionscript"> <span class="keyword">return</span>;</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="actionscript"> <span class="comment">// // 检查图片类型 </span></span></span><br><span class="line"><span class="actionscript"> <span class="comment">// if (!(img.type.indexOf('image') == 0 && img.type && /\.(?:jpg|png|gif)$/.test(img.name))) {</span></span></span><br><span class="line"><span class="actionscript"> <span class="comment">// alert('图片只能是jpg,gif,png');</span></span></span><br><span class="line"><span class="actionscript"> <span class="comment">// return;</span></span></span><br><span class="line"><span class="actionscript"> <span class="comment">// }</span></span></span><br><span class="line"></span><br><span class="line"><span class="actionscript"> <span class="comment">// 检查图片尺寸 </span></span></span><br><span class="line"> if (img.size > 1024 * 1024) {</span><br><span class="line"><span class="actionscript"> alert(<span class="string">'图片不能大于120K'</span>);</span></span><br><span class="line"><span class="actionscript"> <span class="keyword">return</span>;</span></span><br><span class="line"> }</span><br><span class="line"><span class="actionscript"> <span class="comment">// file reader </span></span></span><br><span class="line"><span class="actionscript"> <span class="keyword">var</span> reader = <span class="keyword">new</span> FileReader();</span></span><br><span class="line"> reader.readAsDataURL(img);</span><br><span class="line"><span class="actionscript"> reader.onload = <span class="function"><span class="keyword">function</span> <span class="params">(e)</span> </span>{ <span class="comment">// reader onload start </span></span></span><br><span class="line"></span><br><span class="line"><span class="actionscript"> <span class="keyword">var</span> image = <span class="keyword">new</span> Image();</span></span><br><span class="line"> image.src = e.target.result;</span><br><span class="line"></span><br><span class="line"><span class="actionscript"> image.onload = <span class="function"><span class="keyword">function</span> <span class="params">()</span> </span>{ <span class="comment">// image onload start </span></span></span><br><span class="line"></span><br><span class="line"><span class="actionscript"> <span class="keyword">var</span> img_width = <span class="keyword">this</span>.width;</span></span><br><span class="line"><span class="actionscript"> <span class="keyword">var</span> img_height = <span class="keyword">this</span>.height;</span></span><br><span class="line"></span><br><span class="line"><span class="actionscript"> <span class="comment">// 设置画布尺寸 </span></span></span><br><span class="line"> canvas.width = img_width;</span><br><span class="line"> canvas.height = img_height;</span><br><span class="line"><span class="actionscript"> <span class="comment">// 将图片按像素写入画布 </span></span></span><br><span class="line"><span class="actionscript"> context.drawImage(<span class="keyword">this</span>, <span class="number">0</span>, <span class="number">0</span>, img_width, img_height);</span></span><br><span class="line"><span class="actionscript"> <span class="comment">// document.getElementById("box").appendChild(canvas)</span></span></span><br><span class="line"><span class="actionscript"> <span class="comment">//显示图片</span></span></span><br><span class="line"><span class="javascript"> <span class="built_in">document</span>.getElementById(<span class="string">"box"</span>).appendChild(image)</span></span><br><span class="line"><span class="actionscript"> <span class="comment">// 读取图片像素信息 </span></span></span><br><span class="line"><span class="actionscript"> <span class="keyword">var</span> imageData = context.getImageData(<span class="number">0</span>, <span class="number">0</span>, <span class="number">7</span>, <span class="number">7</span>);</span></span><br><span class="line"><span class="javascript"> <span class="keyword">let</span> len = imageData.data.length / <span class="number">4</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">let</span> r = <span class="number">0</span>,</span></span><br><span class="line"> g = 0,</span><br><span class="line"> b = 0,</span><br><span class="line"> num = [];</span><br><span class="line"><span class="javascript"> <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i < imageData.data.length; i += <span class="number">4</span>) {</span></span><br><span class="line"> r += imageData.data[i];</span><br><span class="line"> g += imageData.data[i + 1];</span><br><span class="line"> b += imageData.data[i + 2];</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"><span class="actionscript"> <span class="comment">// console.log(imageData.data,);</span></span></span><br><span class="line"> num = [r / len, g / len, b / len]</span><br><span class="line"><span class="javascript"> <span class="keyword">let</span> newrgb = [<span class="built_in">Math</span>.floor(num[<span class="number">0</span>] / <span class="number">9</span>), <span class="built_in">Math</span>.floor(num[<span class="number">1</span>] / <span class="number">9</span>), <span class="built_in">Math</span>.floor(num[<span class="number">2</span>] / <span class="number">9</span>)]</span></span><br><span class="line"><span class="javascript"> yurgb = [<span class="built_in">Math</span>.floor((num[<span class="number">0</span>] % <span class="number">9</span>) / <span class="number">6</span>), <span class="built_in">Math</span>.floor((num[<span class="number">1</span>] % <span class="number">9</span>) / <span class="number">6</span>), <span class="built_in">Math</span>.floor((num[<span class="number">2</span>] % <span class="number">9</span>) /</span></span><br><span class="line"> 6)]</span><br><span class="line"><span class="javascript"> <span class="keyword">let</span> allrgb = [(yurgb[<span class="number">0</span>] + newrgb[<span class="number">0</span>]) * <span class="number">9</span>, (yurgb[<span class="number">1</span>] + newrgb[<span class="number">1</span>]) * <span class="number">9</span>, (yurgb[<span class="number">2</span>] + newrgb[<span class="number">2</span>]) *</span></span><br><span class="line"> 9</span><br><span class="line"> ]</span><br><span class="line"><span class="actionscript"> <span class="comment">// console.log('img', num, newrgb, yurgb, allrgb);</span></span></span><br><span class="line"> ary.push(allrgb)</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ind++</span><br><span class="line"> if (ind === eve.files.length) {</span><br><span class="line"><span class="javascript"> <span class="keyword">let</span> inn = <span class="built_in">document</span>.getElementById(<span class="string">"box"</span>);</span></span><br><span class="line"> clearInterval(timer)</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }, 1000);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="tag"></<span class="name">script</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-promise的all,race,finally源码解析" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/08/18/promise%E7%9A%84all%EF%BC%8Crace%EF%BC%8Cfinally%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90/" class="article-date">
<time datetime="2020-08-17T16:50:55.413Z" itemprop="datePublished">2020-08-18</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/08/18/promise%E7%9A%84all%EF%BC%8Crace%EF%BC%8Cfinally%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90/">
promise的all,race,finally源码解析
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h2 id="promise的all,race,finally源码解析"><a href="#promise的all,race,finally源码解析" class="headerlink" title="promise的all,race,finally源码解析"></a>promise的all,race,finally源码解析</h2><h3 id="promise-all源码"><a href="#promise-all源码" class="headerlink" title="promise.all源码"></a>promise.all源码</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//传入为空返回成成功态,所有加载完才会,改成功态,一个失败态就会直接结束</span></span><br><span class="line"> <span class="built_in">Promise</span>.all_ = <span class="function"><span class="keyword">function</span> (<span class="params">promise_</span>) </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =></span> {</span><br><span class="line"> <span class="comment">//array.from,会转为真正的数组,将传入的promise实例组成一个真正的狐族</span></span><br><span class="line"> promise_ = <span class="built_in">Array</span>.from(promise_)</span><br><span class="line"> <span class="keyword">if</span> (promise_.length === <span class="number">0</span>) {<span class="comment">//如果传入的的是一个空数组,会异步的返回一个成功态的promise</span></span><br><span class="line"> resolve([])</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">let</span> result = [];<span class="comment">//准备一个空数组</span></span><br><span class="line"> <span class="keyword">let</span> index = <span class="number">0</span>; <span class="comment">//定义一个index</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i < promise_.length; i++) {<span class="comment">//循环这几个promise</span></span><br><span class="line"> <span class="built_in">Promise</span>.resolve(promise_[i]).then(<span class="function"><span class="params">data</span> =></span> {</span><br><span class="line"> result[i] = data<span class="comment">//给数组增加一项</span></span><br><span class="line"> <span class="keyword">if</span>(++index===promise_.length){</span><br><span class="line"> <span class="comment">//如果所有promise都是fulfilled,promise。all实例才变fulfilled</span></span><br><span class="line"> resolve(result)<span class="comment">//所有promise都获取完数据,length相等,才会执行他</span></span><br><span class="line"> }</span><br><span class="line"> },err=>{</span><br><span class="line"> reject(err);</span><br><span class="line"> <span class="keyword">return</span></span><br><span class="line"> })</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> })</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<h3 id="promise-race源码"><a href="#promise-race源码" class="headerlink" title="promise.race源码"></a>promise.race源码</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Promise</span>.ra_ce = <span class="function"><span class="keyword">function</span>(<span class="params">promises</span>) </span>{</span><br><span class="line"> promises = <span class="built_in">Array</span>.from(promises);</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =></span> {</span><br><span class="line"> <span class="keyword">if</span>(promises.length===<span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">let</span> i=<span class="number">0</span>; i<promises.length; i++) {</span><br><span class="line"> <span class="built_in">Promise</span>.resolve(promises[i]).then(<span class="function"><span class="params">data</span> =></span> {</span><br><span class="line"> resolve(data);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }, err => {</span><br><span class="line"> reject(err);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> })</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> })</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="Promise-finally源码"><a href="#Promise-finally源码" class="headerlink" title="Promise.finally源码"></a>Promise.finally源码</h3><p><strong>不管成功还是失败,都会走到finally中,并且finally之后,还可以继续then。并且会将值原封不动的传递给后面的then。</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Promise</span>.prototype.finally_ = <span class="function"><span class="keyword">function</span> (<span class="params">callback</span>) </span>{</span><br><span class="line"> <span class="comment">// console.log(this);this指向promise这个实例</span></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.then(<span class="function">(<span class="params">value</span>) =></span> {<span class="comment">//需要返回一个promise实例</span></span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(callback()).then(<span class="function"><span class="params">()</span> =></span> {</span><br><span class="line"> <span class="keyword">return</span> value;<span class="comment">//成功态</span></span><br><span class="line"> });</span><br><span class="line"> }, (err) => {<span class="comment">//需要返回一个promise实例</span></span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(callback()).then(<span class="function"><span class="params">()</span> =></span> {</span><br><span class="line"> <span class="keyword">throw</span> err<span class="comment">//失败态</span></span><br><span class="line"> })</span><br><span class="line"> })</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">Promise</span>.prototype.finally_()</span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-React中的Fragment(性能优化)" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/08/15/React%E4%B8%AD%E7%9A%84Fragment%EF%BC%88%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%EF%BC%89/" class="article-date">
<time datetime="2020-08-14T16:13:51.224Z" itemprop="datePublished">2020-08-15</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/08/15/React%E4%B8%AD%E7%9A%84Fragment%EF%BC%88%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%EF%BC%89/">
React中的Fragment(性能优化)
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="React中的Fragment(性能优化)"><a href="#React中的Fragment(性能优化)" class="headerlink" title="React中的Fragment(性能优化)"></a>React中的Fragment(性能优化)</h3><p><strong>代替div作为外层,可做不可见的包裹元素</strong></p>
<ul>
<li>一个组件往往要返回多个元素,同时,react有要求这些元素必须包裹在一个div里</li>
<li>普遍解决办法是用div包裹,这样<code>增加了许多没必要的嵌套,增加了浏览器的渲染压力</code></li>
<li>解决办法:eract 16 开始,ract支持返回数组</li>
</ul>
<figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> React from <span class="string">'react'</span>;</span><br><span class="line"> </span><br><span class="line">export <span class="keyword">default</span> <span class="keyword">function</span> () {</span><br><span class="line"> <span class="keyword">return</span> [</span><br><span class="line"> <span class="xml"><span class="tag"><<span class="name">div</span>></span>一步 01<span class="tag"></<span class="name">div</span>></span></span>,</span><br><span class="line"> <span class="xml"><span class="tag"><<span class="name">div</span>></span>一步 02<span class="tag"></<span class="name">div</span>></span></span>,</span><br><span class="line"> <span class="xml"><span class="tag"><<span class="name">div</span>></span>一步 03<span class="tag"></<span class="name">div</span>></span></span>,</span><br><span class="line"> <span class="xml"><span class="tag"><<span class="name">div</span>></span>一步 04<span class="tag"></<span class="name">div</span>></span></span></span><br><span class="line"> ];</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>常用办法:<strong>Fragments</strong>,用来包裹元素作为最外层,减少dom层级</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <Fragment></span><br><span class="line"> <div>一步 <span class="number">01</span><<span class="regexp">/div></span></span><br><span class="line"><span class="regexp"> <div>一步 02</</span>div></span><br><span class="line"> <div>一步 <span class="number">03</span><<span class="regexp">/div></span></span><br><span class="line"><span class="regexp"> <div>一步 04</</span>div></span><br><span class="line"> <<span class="regexp">/Fragment></span></span><br><span class="line"><span class="regexp"> );</span></span><br><span class="line"><span class="regexp">}</span></span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-面试--React中PureComponent原理与核心源码" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/08/14/%E9%9D%A2%E8%AF%95--React%E4%B8%ADPureComponent%E5%8E%9F%E7%90%86%E4%B8%8E%E6%A0%B8%E5%BF%83%E6%BA%90%E7%A0%81/" class="article-date">
<time datetime="2020-08-14T15:31:18.081Z" itemprop="datePublished">2020-08-14</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/08/14/%E9%9D%A2%E8%AF%95--React%E4%B8%ADPureComponent%E5%8E%9F%E7%90%86%E4%B8%8E%E6%A0%B8%E5%BF%83%E6%BA%90%E7%A0%81/">
面试--React中PureComponent原理与核心源码
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="React中PureComponent原理与核心源码"><a href="#React中PureComponent原理与核心源码" class="headerlink" title="React中PureComponent原理与核心源码"></a>React中PureComponent原理与核心源码</h3><ul>
<li><strong>PureComponent 会对 <code>props</code> 和 <code>state</code> 进行浅层比较,并减少了跳过必要更新的可能性。</strong></li>
<li><strong>React.PureComponent 与 React.Component 很相似。两者的区别在于 React.Component 并未实现 <code>shouldComponentUpdate()</code>,而 React.PureComponent中以浅层对比 prop 和 state 的方式来实现了该函数。</strong></li>
<li>** nextState.items === prevState.items 为 true进行浅层判断,会导致重新render**</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React, { Component } <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">shallowEqual</span>(<span class="params">obj1, obj2</span>) </span>{<span class="comment">//传入新老state和prop</span></span><br><span class="line"> <span class="keyword">if</span> (obj1 === obj2) {<span class="comment">//一样直接不改变</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (<span class="keyword">typeof</span> obj1 !== <span class="string">'object'</span> || obj1 === <span class="literal">null</span> || <span class="keyword">typeof</span> obj2 !== <span class="string">'object'</span> || obj2 === <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span><span class="comment">//检测,如果有一个满足就更新</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">let</span> keys1 = <span class="built_in">Object</span>.keys(obj1)<span class="comment">//拿到所有属性名组成的数组</span></span><br><span class="line"> <span class="keyword">let</span> keys2 = <span class="built_in">Object</span>.keys(obj2)<span class="comment">//拿到所有属性名组成的数组</span></span><br><span class="line"> <span class="keyword">if</span> (keys1.length !== keys2.length) {</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span><span class="comment">//如果键值对数量不相等,那么更新</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">const</span> key <span class="keyword">of</span> keys1) {<span class="comment">//循环进行比较</span></span><br><span class="line"> <span class="keyword">if</span> (!obj2.hasOwnProperty(key) || obj1[key] !== obj2[key]) {</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span><span class="comment">//如果obj2没有key的值,或者表层的key的值不相等时,更新</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">true</span><span class="comment">//走到这里,说明没改变,那么不更新</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PureComponent</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>{<span class="comment">//引用shouldcomponentupdate方法</span></span><br><span class="line"> shouldComponentUpdate(nextProps, nextState) {<span class="comment">//传入的nextprops,nextState与这次的比较,并且传入</span></span><br><span class="line"> <span class="keyword">return</span> !shallowEqual(<span class="keyword">this</span>.props, nextProps) || !shallowEqual(<span class="keyword">this</span>.state, nextState)</span><br><span class="line"> <span class="comment">//返回值是ture就更新组件,false就不更新组件,这里调用手写shallowEqual方法</span></span><br><span class="line"> }</span><br><span class="line"> render() {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.props.children;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> PureComponent;</span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-面试—provide与inject用法" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2020/08/14/%E9%9D%A2%E8%AF%95%E2%80%94provide%E4%B8%8Einject%E7%94%A8%E6%B3%95/" class="article-date">
<time datetime="2020-08-14T14:01:03.438Z" itemprop="datePublished">2020-08-14</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2020/08/14/%E9%9D%A2%E8%AF%95%E2%80%94provide%E4%B8%8Einject%E7%94%A8%E6%B3%95/">
面试—provide与inject用法
</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>##面试—provide与inject用法</p>
<h3 id="爷孙组件传递"><a href="#爷孙组件传递" class="headerlink" title="爷孙组件传递"></a>爷孙组件传递</h3><p>不存在双向绑定,用inject注入的<br><strong>以允许一个祖先组件向其所有子孙后代注入一个依赖,不论组件层次有多深,并在起上下游关系成立的时间里始终生效。</strong></p>
<ul>
<li>使用场景:由于vue有$parent属性可以让子组件访问父组件。但孙组件想要访问祖先组件就比较困难。通过provide/inject可以轻松实现跨级访问祖先组件的数据</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//祖宗注入。provide</span></span><br><span class="line"><template></span><br><span class="line"> <div id=<span class="string">"app"</span>></span><br><span class="line"> <<span class="regexp">/div></span></span><br><span class="line"><span class="regexp"></</span>template></span><br><span class="line"> <script></span><br><span class="line"> <span class="keyword">export</span> <span class="keyword">default</span> {</span><br><span class="line"> data () {</span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> datas: [</span><br><span class="line"> {</span><br><span class="line"> id: <span class="number">1</span>,</span><br><span class="line"> label: <span class="string">'产品一'</span></span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> 、,</span><br><span class="line"> provide {</span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> datas: <span class="keyword">this</span>.datas</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <<span class="regexp">/script></span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//后代通过inject都可以拿到</span></span><br><span class="line"><template></span><br><span class="line"> <div></span><br><span class="line"> <ul></span><br><span class="line"> <li v-<span class="keyword">for</span>=<span class="string">"(item, index) in datas"</span> :key=<span class="string">"index"</span>></span><br><span class="line"> {{ item.label }}</span><br><span class="line"> <<span class="regexp">/li></span></span><br><span class="line"><span class="regexp"> </u</span>l></span><br><span class="line"> <<span class="regexp">/div></span></span><br><span class="line"><span class="regexp"></</span>template></span><br><span class="line"> <script></span><br><span class="line"> <span class="keyword">export</span> <span class="keyword">default</span> {</span><br><span class="line"> inject: [<span class="string">'datas'</span>]</span><br><span class="line"> }</span><br><span class="line"> <<span class="regexp">/script></span></span><br></pre></td></tr></table></figure>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<nav id="page-nav">
<span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><a class="extend next" rel="next" href="/page/2/">Next &raquo;</a>
</nav>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info">
<div class="footer-left">
© 2021 纪安
</div>
<div class="footer-right">
<a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/smackgg/hexo-theme-smackdown" target="_blank">Smackdown</a>
</div>
</div>
</div>
</footer>
</div>
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
<script>
var yiliaConfig = {
fancybox: true,
mathjax: true,
animate: true,
isHome: true,
isPost: false,
isArchive: false,
isTag: false,
isCategory: false,
open_in_new: false
}
</script>
<script src="/js/main.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}
});
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>
</div>
</body>
</html>