【问题标题】:es6 setprops is not a functiones6 setprops 不是函数
【发布时间】:2016-08-11 06:56:11
【问题描述】:
class MyComponent {
    constructor(props){
        super(props);
    }
    render() {
        return <View />
    }
}

class Test() {
    constructor(props){
        super(props);
        this.onClick = this.onClick.bind(this);
    }

    onClick() {
        this.my.setPorps({
            show: true,
        });
    }

    render(){
        return <MyComponent ref={ ref => this.my = ref }/>
    }
}

如果我触发onClick函数,会导致:

undefined 不是函数(评估 '_this3.progressbar.setProps({ 禁用:true})')

谁能帮帮我?非常感谢!

【问题讨论】:

    标签: reactjs react-native ecmascript-6


    【解决方案1】:

    来自 React 文档:

    此方法已弃用,很快将被删除。此方法在扩展 React.Component 的 ES6 类组件上不可用。不要调用 setProps,而是尝试使用新的 props 再次调用 ReactDOM.render()。

    https://facebook.github.io/react/docs/component-api.html#setprops

    (同样在您的示例中,您拼写为setPorps,但无论如何它都不起作用)

    看起来你只是想告诉一个组件显示自己,在这种情况下,父状态可以更新并作为道具传递给子:

    class Test() {
        constructor(props){
            super(props);
            this.state = {
                show: false
            };
            this.onClick = this.onClick.bind(this);
        }
    
        onClick() {
            this.setState({
                show: true
            });
        }
    
        render() {
            return <MyComponent show={this.state.show} />
        }
    }
    

    【讨论】:

    • 好吧,我怎样才能重新渲染部分组件而不是重新渲染所有 dom。
    • 你到底想达到什么目的?
    • 只想重新渲染部分dom而不是全部dom。因为,这会花费很多时间。
    • 如果改变了父 state 并且一个子 prop 发生了变化,React 只会渲染 child 中改变的内容(如果 prop 影响了 html 输出),这和 setProps 没有区别跨度>
    • 但是它会比较所有的dom。
    猜你喜欢
    • 2017-10-05
    • 1970-01-01
    • 2016-07-03
    • 2014-12-05
    • 1970-01-01
    • 2019-10-25
    • 2017-01-04
    • 2017-04-28
    • 2019-03-11
    相关资源
    最近更新 更多