繁体   English   中英

反应本机redux无法正确更新道具

[英]React native redux not updating props properly

我有一个简单的react native组件,当我单击按钮时,它会触发一个动作,并且我希望它从同一组件中更新道具。

我正在做这样的事情:

component.js

class TypeButton extends Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <Button
                name = "Type"
                render = {this.customRender.bind(this)}
                onPress = {this.onPress.bind(this)}
                size={60}
            />
        )
    }

    onPress() {
        let type = this.props.type == FREE ? PAID : FREE;

        this.props.userInputAction({
            type : type
        });
    }

    customRender() {
        return (
            <Text> {this.props.type} </Text>
        )
    }
}

function mapStateToProps(state, ownProps) {
    if (state.userInputReducer)
        return state.userInputReducer.info;
    return {};
}

function mapDispatchToProps(dispatch) {
    return bindActionCreators(actions, dispatch);
}

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

action.js

   export function userInput(info) {
        return {
            type : types.USER_INPUT,
            info
        }
    }

reducer.js

我为样板代码创建了一个create reducer函数:

export const userInputReducer = createReducer(
    {
        info : {
            type : constants.FREE
        }
    },
    {
        [types.USER_INPUT](state, action) {
            let info = action.info;
            if (state.info)
                info = _.assign(state.info, action.info);

            return {
                info
            }
        }
    }
)

问题在于,在mapStateToProps this.props.type的值已更改,但是在customRender this.props.type具有旧的值,并且未更新。 你知道我在做什么错吗?

注意:

如果从mapStateToProps返回一个包含类型的对象,例如:

function mapStateToProps(state, ownProps) {
    return state.userInputReducer;
}

和替换遍布this.props.typethis.props.info.type一切工作正常? 我真的不明白问题是什么吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM