父级的setState不会触发子级的componentWillReceiveProps

时间:2018-09-09 21:00:33

标签: javascript reactjs react-native state react-props

我有一个组件父级和一个组件子级,其中一些道具连接到父级状态。

在父级中,我调用setState,但未触发子级的componentWillReceiveProps函数。
更确切地说,它是在父对象的某个点触发的,而不是在另一点被触发的。

这是父母:

... imports

class HomeScreen extends Component {

    constructor(props) {
        super(props);

        dispatchFbPermissionAction = this.dispatchFbPermissionAction.bind(this);

        this.state = {
            fbPermissions: [],
        }
    }

    componentDidMount () {
        this._loadInitialState();
    }

    _responsePermissionsCallback(error: ?Object, result: ?Object) {
        if (error) {
            log('Error fetching data: ' + error.toString());
        } else {
            dispatchFbPermissionAction(result.data);
        }
    }

    dispatchFbPermissionAction = (data) => {
        // **NOT FIRED**
        this.setState({
            fbPermissions: data
        });

        this.props.fbPermissionsLoaded(data);
    }

    async _loadInitialState() {
        AccessToken.getCurrentAccessToken().then(
            (data) => {
                if (data) {
                    const infoRequest = new GraphRequest(
                      '/me/permissions',
                      null,
                      this._responsePermissionsCallback,
                    );
                    new GraphRequestManager().addRequest(infoRequest).start();

                    // **FIRED**
                    this.setState({
                        ...
                    });

                    this.props.loggedIn();
                }
            }
        );
    }

    render () {
        const { navigation } = this.props;

        return (
            <Container>
                <ScrollableTabView
                    <View tabLabel="ios-film" style={styles.tabView}>
                        <Text style={styles.tabTitle}>{_.toUpper(strings("main.theatres"))}</Text>
                        <ListTheatre navigation={this.props.navigation} filterText={this.state.filterText} isLoggedIn={this.state.isLoggedIn} fbPermissions={this.state.fbPermissions}></ListTheatre>
                    </View>
                </ScrollableTabView>
            </Container>
        )
    }
}

const mapStateToProps = (state) => {
    return {
        isLoggedIn: state.isLoggedIn,
        listTheatre: state.listTheatre,
        listMusic: state.listMusic
    };
};

// wraps dispatch to create nicer functions to call within our component
const mapDispatchToProps = (dispatch) => ({
    startup: () => dispatch(StartupActions.startup()),
    loggedIn: () => dispatch({
        type: LOGGED_IN
    }),
    fbPermissionsLoaded: (data) => dispatch({
        type: FB_PERMISSIONS_LOADED,
        fbPermissions: data
    })
});

export default connect(mapStateToProps, mapDispatchToProps)(HomeScreen)

这是孩子:

... imports

class ListTheatre extends Component {

    constructor(props) {
        super(props);

        this.state = {
            ...
        }
    }

    componentWillReceiveProps(nextProps) {
        log(this.props)
    }

    shouldComponentUpdate(nextProps, nextState) {
        return !nextState.fetching;
    }

    render() {
        const { navigate } = this.props.navigation;

        return (
            <SectionList
                ...
            />
        )
    }
}

ListTheatre.propTypes = {
    isLoggedIn: PropTypes.bool.isRequired,
}

const mapStateToProps = (state) => {
    return {
        isLoggedIn: state.isLoggedIn
    };
};

const mapDispatchToProps = (dispatch) => ({
    startup: () => dispatch(StartupActions.startup())
});

export default connect(mapStateToProps, mapDispatchToProps)(ListTheatre);

我不理解为什么GraphRequestManager().addRequest调用后的setState像超级按钮一样工作(子级的componentWillReceiveProps函数被触发),而dispatchFbPermissionAction函数中的setState不会触发子级的componentWillReceiveProps函数。 / p>

1 个答案:

答案 0 :(得分:2)

这是由于connect / Connect(ListTheatre)封装了您的ListTheatre组件为内部内部实现的sCU(shouldComponentUpdate),通过将connect的纯选项设置为false将其关闭喜欢

export default connect(mapStateToProps, mapDispatchToProps, null, {pure: false})(ListTheatre)

  

[pure](布尔值):如果为true,则connect()将避免重新渲染和调用mapStateToProps,mapDispatchToProps和mergeProps,如果相关的state / props对象根据各自的相等性检查保持相等。假设包装的组件是“纯”组件,除了其道具和所选Redux商店的状态外,不依赖任何输入或状态。默认值:true