javascript - 无法让 shouldComponentUpdate() 在 native react 中工作

标签 javascript reactjs react-native

shouldComponentUpdate 在 React Native 中不起作用,还是我遗漏了什么?我只是想记录参数以便我可以比较,但它甚至没有被记录。当我运行 JS 时,我看到的只是“######## 渲染被调用”。

import React, { Component } from 'react';

class MyComponent extends Component {
    constructor(props) {
        super(props);
        this.state = {}
    }

    shouldComponentUpdate(nextProps, nextState) {
        console.log("######### shouldComponentUpdate called");
        console.log(nextProps);
        return true;
    }

    render() {
        console.log("######### render called");
        return <SomeJSX>
    }
}

export default MyComponent;
  

最佳答案

shouldComponentUpdateupdate 阶段的一部分,因此您必须更改 propsstate 才能获取所需的输出。查看文档 here

关于javascript - 无法让 shouldComponentUpdate() 在 native react 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49269705/

相关文章:

javascript - 更改事件处理程序仅触发一次,jQuery

javascript - 页面滚动和 JQuery

php - 如何在php中回显ascii加密文本

javascript - 我可以将状态属性设置为回调吗?

react-native - 如何在 React Native 中设置 TextInput 的样式以进行密码输入

react-native - 了解 react native 复活的执行

javascript - 调用 ajax 调用序列的模式?

reactjs - 作为函数或 const 的无状态组件

html - 使用 Flexbox 的等高卡片

javascript - 导航器组件是 react 原生应用程序中切换屏幕的唯一方法吗?