gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:19:58 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com