鍍金池/ 問答/HTML/ React v16.3.2中g(shù)etDerivedStateFromProps 無

React v16.3.2中g(shù)etDerivedStateFromProps 無效?

已經(jīng)將react升級(jí)到16.3.2版本了

"react": "^16.3.2",
"react-dom": "^16.3.2",

進(jìn)行了方法改寫:

// brfore
componentWillReceiveProps(nextProps: Props) {
        console.log(nextProps);
        this.setState({
            value: setCurrentStyleToSelect(nextProps, this.props.regex, this.props.defaultValue),
        });
    }
    
// now
static getDerivedStateFromProps(nextProps: Props, prevState: Props) {
    console.log(nextProps);
    console.log(prevState);
    if (nextProps !== prevState) {
        return {
            value: setCurrentStyleToSelect(
                    nextProps,
                    prevState.regex,
                    prevState.defaultValue,
                ),
        };
    }
    return null;
}

現(xiàn)在的console進(jìn)不去;getDerivedStateFromProps中的方法都沒有執(zhí)行到

回答
編輯回答
拽很帥

你把node_modules文件夾和package-lock.json都刪除了,重新npm install 一下。

2018年6月6日 21:14