首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无限循环Redux / React?

无限循环Redux / React?
EN

Stack Overflow用户
提问于 2018-06-19 06:44:13
回答 1查看 0关注 0票数 0

我有主要组件包装其他组件。

代码语言:javascript
复制
<Configuration>
    <Switch>
      .....
    </Switch>
</Configuration>

如果配置容器我使用连接:

代码语言:javascript
复制
const mustBe = ['menu'];

function mapStateToProps (state) {
  return {...state.configuration}
}


function mapDispatchToProps(dispatch){
    return {
        actions: bindActionCreators({
            ...upload
        }, dispatch),
        dispatch
    }
}

class Configuration extends React.Component {

    constructor(props, context){
        super(props, context);

        this.state = {uploaded: false}

    }

    componentDidMount(){

        for(let key of mustBe){

            if(!this.props[key] || !Object.keys(this.props[key]).length ){
                this.props.actions.upload()
            }
        }

    }

    componentWillReceiveProps(){
        this.setState((state) => {
            return {uploaded: true}
        })
    }


    render(){

        if(!this.state.uploaded) return null;

        return {this.props.children};
        )
    }
}

Configuration.contextTypes = {
    router: PropTypes.object
}

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Configuration));

配置有自己的减速器,包括菜单,例如:

代码语言:javascript
复制
import menu from './menu.rd.js';


const Configuration = combineReducers({
  menu
});

很简单。

另外我也有使用连接的菜单组件:

代码语言:javascript
复制
function mapStateToProps (state) {
  return {...state.configuration.menu}
}

function mapDispatchToProps(dispatch){
    return {
        actions: bindActionCreators({
            ...menu
        }, dispatch),
        dispatch
    }
}


class Menu extends React.Component {
    constructor(props, context){
        super(props, context);
    }

    componentDidMount(){

        let {pathname} = this.context.router.route.location;
        this.props.actions.changed(pathname);

    }

    render(){

        return (
            ...
        )
    }
}

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

问题是:重新启动后,所有从容器开始,我加载菜单项和呈现子组件。

子组件包含componentDidMount和render方法,在组件呈现后调用componentDidMount并更改存储中的状态。

再次调用componentWillReceiveProps并渲染。

在组件调用Render和componentDidMount中再次改变状态。

它无限循环。你用它做什么?为什么componentDidMount会随时调用渲染?

EN

回答 1

Stack Overflow用户

发布于 2018-06-19 15:58:34

我在路由器中使用循环,并始终调用组件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档