react 嵌套渲染_不为嵌套组件调用React渲染方法(React render method not called for nested component)...

在App组件中,当尝试渲染包含Chart和Bar子组件的图表时,发现在Bar组件中shouldComponentUpdate生命周期方法未被调用,因此render方法也没有执行。文章探讨了这个问题,并检查了相关组件的代码,特别是Bar组件的数据比较逻辑和状态更新。
摘要由CSDN通过智能技术生成

I have these React classes

class App extends React.Component {

constructor(props) {

super(props);

}

componentDidMount() {

const { dispatch } = this.props;

var props = this.props;

dispatch(fetchDistricts('California'));

}

render() {

return (

height={this.props.height}>

width={this.props.width}

height={this.props.height}>

);

}

};

Chart looks like this

export default class Chart extends React.Component {

render() {

return (

height={this.props.height}>

);

}

};

And Bars looks like this

export default class Bar extends React.Component {

shouldComponentUpdate(nextProps) {

debugger

return this.props.data !== nextProps.data;

}

render() {

debugger

let props = this.props;

let data = props.data.map((d) =>

{

return d.y;

}

);

let yScale = d3.scale.linear()

.domain([0, d3.max(data)])

.range([0, this.props.height]);

let xScale = d3.scale.ordinal()

.domain(d3.range(this.props.data.length))

.rangeRoundBands([0, this.props.width], 0.05);

let bars = data.map((point, i) => {

var height = yScale(point),

y = props.height - height,

width = xScale.rangeBand(),

x = xScale(i);

return (

width={width}

x={x}

y={y}

key={i}>

);

});

return (

{bars}

);

}

};

the debuggers in Bars aren't called - the render method is not called.

Why?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值