反应生命周期挂钩不适用于redux状态更新

时间:2019-09-17 07:37:12

标签: reactjs redux react-redux

我正在使用带有typescript和react-redux包的react。

按照文档中的说明配置组件-https://react-redux.js.org/introduction/quick-start

console.log正在使用连接功能,但组件生命周期无效。

  

container.ts

import Toolbar from './Toolbar.Component';


export default connect<any, any, any>((store:any) => {
    console.log('++++++++++++++++++++',store.toolbarComponent);
    return store.toolbarComponent
}, {})(Toolbar)```

  

component.ts

export default class ToolbarComponent extends React.Component<Props, State> {  

    constructor(props: Props) {
        super(props);
        this.state = {
            activeProperty: '',
            toolbarList: this.props.toolbarList
        };
        console.log('--- comp init ---', this.state);
        this.dispatchData = this.dispatchData.bind(this);
    }

    UNSAFE_componentWillUpdate(){
        console.log('-----', this.props.toolbarList, this.state.toolbarList)
    }

    componentWillUpdate(){   
        console.log('-----', this.props.toolbarList, this.state.toolbarList)
    }

    componentWillReceiveProps(){
        console.log('-----', this.props.toolbarList, this.state.toolbarList)
    }
    componentDidUpdate(){
        console.log('-----', this.props.toolbarList, this.state.toolbarList)
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用传播算子

    let font = UIFont.systemFont(ofSize: 15);

    let style = NSMutableParagraphStyle();
    style.lineBreakMode = .byWordWrapping;

    let attributes: [NSAttributedString.Key: Any] = [
        .font: font,
       .paragraphStyle: style,
    ];

    let attributedString = NSAttributedString(string: getPromptText(), attributes: attributes)

    lblDisplayText.setAttributedText(attributedString);

说明-组件挂钩未检测到import Toolbar from './Toolbar.Component'; export default connect<any, any, any>((store:any) => { return { ...store.toolbarComponent } <-- Here }, {})(Toolbar) 对象中的更改,并且散布运算符克隆了store.toolbarComponent对象并传递了克隆的版本。