1

我有一个组件,如果它的 prop.paramA 与存储在 cookie 中的 paramA 值不同,它就会呈现。

以下是我尝试将 cookie 管理逻辑移动到单独的组件中。问题是问题组件仅在 showQuestion 为 false 时呈现一次,所以我永远看不到问题。

目前,我的代码如下所示

// question.js
import React from 'react';
import ToggleDisplay from 'react-toggle-display';
import {withCookies, Cookies} from 'react-cookie';

class Question extends React.Component {

    static get propTypes() {
        return {
            cookies: instanceOf(Cookies).isRequired
        }
    }

    constructor(props) {
        super(props);
        this.state = {
            paramA: props.paramA,
            showQuestion: props.showQuestion
        };
    }

    componentDidMount() {
        if (this.state.showQuestion) {
            // do some ajax calls etc.
        }
    }

    render() {
        return (
            <div id="simplequestion">
                <ToggleDisplay show={this.state.showQuestion}>
                    <div>What is your SO profile?</div>
                </ToggleDisplay>
            </div>
        );
    }
}

export default withCookies(Question);

我写了以下 CookieManager 组件-

// cookiemanager.js
import React from 'react';
import {withCookies, Cookies} from 'react-cookie';

class CookieManager extends React.Component {
    static get propTypes() {
        return {
            cookies: instanceOf(Cookies).isRequired
        }
    }

    constructor(props) {
        super(props);
        let propA = props.cookies.get('propA');
        if (!propA || propA !== props.propA) {
            props.cookies.set('propA', props.propA, { path: '/', maxAge: 604800});
            console.log('changed', propA, props.propA);
            props.propAChanged(true);
        } else if (propA === props.propA) {
            console.log('unchanged', propA, props.propA);
            props.propAChanged(false);
        }
    }
    render() { 
        return <div id="cookieManager"></div>;
    }
}
export default withCookies(CookieManager);

以下是顶级应用程序

// app.js
import React from 'react';
import Question from './question';
import CookieManager from './cookiemanager';
import { CookiesProvider } from 'react-cookie';


class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            paramA: props.paramA,
            showQuestion: false,
            avoidUpdate: false
        };
        this.updateShowQuestion = this.updateShowQuestion.bind(this);
    }

    updateShowQuestion(x) {
        if (this.state.avoidUpdate) {
            return;
        }
        this.setState({
            paramA: this.state.paramA,
            showQuestion: x,
            avoidUpdate: !this.state.avoidUpdate
        });
    }

    componentWillUpdate(nextProps, nextState) {
        if (!nextState.avoidUpdate && nextState.paramA === this.state.paramA) {
            this.setState({
                paramA: this.state.paramA,
                showQuestion: this.state.showQuestion,
                avoidUpdate: true
            });
        }
    }

    render() {
        return (
            <CookiesProvider>
                <CookieManager paramA={this.state.paramA} ridChanged={this.updateShowQuestion}>
                <Question
                    paramA={this.state.paramA}
                    showQuestion={this.state.showQuestion}/>
                </CookieManager>
            </CookiesProvider>
        );
    }
}
export default App;
4

1 回答 1

1

您正在阅读这里的状态:

<ToggleDisplay show={this.state.showQuestion}>

但是showQuestion在构造函数中只设置一次:

this.state = {
    paramA: props.paramA,
    showQuestion: props.showQuestion
};

this.props更改您的组件被渲染时,但您仍在读取this.state未更新的组件,因此可见输出是相同的。

我看到两种方法可以解决这个问题:

  • this.state.showQuestion每当道具更改时更新:

    static getDerivedStateFromProps(nextProps, prevState) {
        if(nextProps.showQuestion !== prevState.showQuestion)
            return { showQuestion };
    }
    
  • render()中,读自this.props.showQuestion

    <ToggleDisplay show={this.props.showQuestion}>
    

    这种方式可能看起来更容易,但使用另一种方式,您可以获得对状态更改的更细粒度的控制。


您错过的另一件事在这里找到:

componentWillUpdate(nextProps, nextState) {
    if (!nextState.avoidUpdate && nextState.paramA === this.state.paramA) {
        this.setState({
            paramA: this.state.paramA,
            showQuestion: this.state.showQuestion, // this line
            avoidUpdate: true
        });
    }
}

您正在接收新的道具,但您正在设置旧状态:showQuestion: this.state.showQuestion. 相反,showQuestion从新道具 - 设置新值nextProps,这样:

componentWillUpdate(nextProps, nextState) {
    if (!nextState.avoidUpdate && nextState.paramA === this.state.paramA) {
        this.setState({
            paramA: this.state.paramA,
            showQuestion: nextProps.showQuestion, // this line
            avoidUpdate: true
        });
    }
}
于 2018-05-03T09:57:25.757 回答