0

我有一个标题,它应该在其状态发生变化时更改其内部组件,例如当用户单击登录按钮时,用户配置文件导航会替换该按钮。

我的按钮是导航组件的一部分,我不知道如何从该导航组件更新其容器组件的状态。

这是我的商店目前的样子

 var React = require('react');
var Reflux = require('reflux');
var LoginActions  = require('../actions/loginbutton.js');

var LoginStore = Reflux.createStore({

    listenables: LoginActions,

    getInitialState: function() {

        return {
                    loggedin : false,
                    links: [{
                                "name": "Link1",
                                "url": "http://www.google.com"
                             },
                                {
                                    "name": "Link2",
                                    "url": "http://www.google.com"

                             },
                            {
                                "name": "Link3",
                                "url": "http://www.google.com"
                             }]

              };


    },

    onLogin: function() {

      /*How do i replace the state for the header*/

      Header.replaceState({
            loggedin: true,
            data: {
                    "userName": "John Doe",
                    "profile": "http://www.google.com",
                    "img": "https://secure.gravatar.com/avatar/87f33484c35375ea9c8fddd2f7e91ee5?d=https://d3rpyts3de3lx8.cloudfront.net/hackerrank/assets/gravatar.jpg&s=200",
                    "email": "maisnamraju@gmail.com",
                    "links": [{
                            "name": "Settings",
                            "url": "/settings"
                    },
                        {
                            "name": "Profile",
                            "url": "/dashboard"
                    },
                        {
                            "name": "Account",
                            "url": "/Account"
                    },
                        {
                            "name": "Logout",
                            "url": "/logout"
                    }]
            }

      });

      console.log(this.state);

    }

});


module.exports = LoginStore;

这是我的标题组件

    var React = require('react');
var Navigation = require('./navmenu.js');
var UserNav = require('./usermenu.js');
var ReactAddons = require('react/addons');
var Reflux = require('reflux');
var LoginStore = require('../stores/loginstore.js');
var LoginActions = require('../actions/loginbutton.js');

var LoggedOut = React.createClass({

    props: {

        navs: React.PropTypes.array
    },

    render: function() {

          return(
                <div>
                      <div className = "nav navbar-nav navbar-right" >
                            <button onClick={LoginActions.Login} className = "login btn btn-primary" > <i className = "fa fa-facebook" > </i> Log In</button >
                      </div>
                      <div className = "collapse navbar-collapse left-bar" >
                            <Navigation classNames = "notloggedin nav navbar-nav" navs = {this.props.navs} />
                       </div>
                 </div>

            );

    }

});


var LoggedIn = React.createClass({

    props: {
        navs: React.PropTypes.array,
        userName: React.PropTypes.string
    },

    render: function() {

        return(
            <div className = "nav navbar-nav navbar-right" >
                  <UserNav navs = {this.props.links}  userName={this.props.userName} />
            </div>
        );
    }
});



var Header = React.createClass({

    mixins: [Reflux.connect(LoginStore, 'data')],

    render: function () {

        var LoggedInState = this.state.data.loggedin;

        if(LoggedInState === 'false' || LoggedInState === false ) {

            LogInArea = <LoggedOut navs={this.state.data.links} />

        }else {

            LogInArea = <LoggedIn navs={this.state.data.data.links} userName={this.state.data.data.userName}/>
        }

        return (

                <div id = { this.props.headerId } >
                    <div className = "navbar navbar-default navbar-fixed-top" >
                        <div className = "container" >
                            <div className = "navbar-header" >
                                    <button type = "button" className = "navbar-toggle collapsed" data-toggle = "collapse" >
                                        <span className = "sr-only"> Toggle navigation < /span >
                                             <span className = "icon-bar" > < /span >
                                             <span className = "icon-bar" > < /span >
                                             <span className = "icon-bar" > < /span >
                                    </button >
                                    <a href = "#" >
                                            <img src="https://www.hackerrank.com/assets/brand/h_mark_sm.png" />
                                     </a>
                              </div>
                              { LogInArea }
                         </div>
                    </div>
                </div>
        );

    }

});


module.exports = Header;

当我设置日期的初始状态时,渲染初始标题没有问题,但是我如何从 LoggedOut 组件内部定位它的状态,以便它设置存储内部的状态?

4

1 回答 1

1

要修改商店的状态,您应该分派action您之前使用Reflux.createActionsfrom 没有您的LoggedOut组件创建的一个。

假设您的操作被称为logout,您将像这样创建它:

var logout = Reflux.createAction();

为了让您的商店做出反应,您必须听取行动:

var statusStore = Reflux.createStore({
  init: function() {
    this.listenTo(logout, this.handleLogout);
  },

  handleLogout: function () {
    // set whatever state you want here
  }
});

现在,您在组件中所要做的就是调用该logout函数。如果您没有同构应用程序,您可以将其作为道具传递,或者使用全局存储。

于 2015-04-03T06:06:34.377 回答