首页 文章

反应更新孩子而不是直接父母

提问于
浏览
2

我有一个看起来像这样的React组件层次结构:

var A = React.createClass({
    render: function() {
        /* stuff */
    }
});    
var B = React.createClass({
    render: function() {
        return dom.div(null, this.props.children);
    }
});    
var C = React.createClass({
    render: function() {
        return dom.div(null, B(null, A()));
    }
});

基本上,C将A元素作为子元素传递给B,B随后将其呈现 .

现在我处在一个我想要更新A元素而不是B的位置 . 它是's unclear to me what the semantics of shouldComponentUpdate are in this case. If I implement a shouldComponentUpdate in B, do I need to consider whether or not my children should update? Is it impossible to update the child without updating it'的直接父级?

1 回答

  • 0

    如果你想更新A而不是B,你应该在它自己的组件中实际描述一个行为 - 例如,在A的 componentDidMount 钩子中订阅一些第三方真实来源(按Flux架构存储) .

    这是唯一的方法 - 如果A无法控制它自己的流量,它只会在B(exectly parent) render() 调用上更新 - 你已经可以通过B 's (and \ or it' s父母控制了它们) shouldComponentUpdate .

相关问题