" shouldComponentUpdate"的默认实现是什么? React组件中的生命周期方法

时间:2015-11-01 20:07:50

标签: javascript reactjs

作为React组件生命周期的一部分,shouldComponentUpdate()方法的自定义实现不是必需的。

我理解它是一个布尔函数,用于确定组件render()props中的更改是否会调用statemixins类似shouldComponentUpdate() 3}}实现var mraa = require('mraa'); //require mraa console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console //var myOnboardLed = new mraa.Gpio(3, false, true); //LED hooked up to digital pin (or built in pin on Galileo Gen1) var myOnboardLed = new mraa.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Intel Galileo Gen2 as well as Intel Edison) myOnboardLed.dir(mraa.DIR_OUT); //set the gpio direction to output var ledState = true; //Boolean to hold the state of Led var PushButton = new mraa.Gpio(3); PushButton.dir(mraa.DIR_IN); var PushButtonState = 1; ButtonCheck(); //periodicActivity(); //call the periodicActivity function function ButtonCheck() { PushButtonState=PushButton.read(); if(PushButtonState==1){ LEDON();} if(PushButtonState==0){ LEDOFF();} } function LEDON() { // process.stdout.write(./ffmpeg -f alsa -thread_queue_size 8388608 -ac 1 -i hw:2 -f video4linux2 -thread_queue_size 8388608 -s 640x480 -r 15 -itsoffset 00:01:00.0 -i /dev/video0 -c:v libxvid -vbr 3 -preset ultrafast -//threads 0 -vsync vfr /media/sdcard/libxvidtestRDY18.avi); ledState = 1; //invert the ledState myOnboardLed.write(ledState?1:0); //if ledState is true then write a '1' (high) otherwise write a '0' (low) setTimeout(ButtonCheck,10); //call the indicated function after (10 milliseconds) } function LEDOFF() { ledState = 0; //invert the ledState myOnboardLed.write(ledState?1:0); //if ledState is true then write a '1' (high) otherwise write a '0' (low) setTimeout(ButtonCheck,10); //call the indicated function after 10 milliseconds) }

如果没有提供自定义实现和mixins。什么是默认的实施和行为?

3 个答案:

答案 0 :(得分:5)

从React v0.13和v0.14开始,默认实现等于null并按照以下逻辑:

    var shouldUpdate =
      this._pendingForceUpdate ||
      !inst.shouldComponentUpdate ||
      inst.shouldComponentUpdate(nextProps, nextState, nextContext);

每个渲染周期都会更新组件(因为!inst.shouldComponentUpdate评估为true)。

答案 1 :(得分:1)

默认为true。

默认行为是在每次状态更改时重新呈现,并且在大多数情况下,您应该依赖默认行为。

当接收到新的道具或状态时,

shouldComponentUpdate()在渲染之前被调用。默认为true。初始渲染或使用forceUpdate()时不会调用此方法。

答案 2 :(得分:0)

鉴于文档React Docs,默认值为true

  

在收到新的道具或州时,在渲染之前调用shouldComponentUpdate()。默认为true。