根据另一个点击的兄弟ReactJS更新兄弟状态

时间:2016-09-07 23:19:18

标签: javascript html reactjs react-router jsx

下午好,

我无法更改兄弟姐妹的组件状态。基本上他们看起来像这样:

<Navigation>
  <NavEndpt /> About <--- no
  <NavEndpt /> Blog
  <NavEndpt /> Projects
</Navigation>

每个<NavEndpt />都是一个链接,用于指向不同的页面&#39;或者在我的网站上路由,除非它是第一个列出的,&#34;关于&#34;。关于我想成为一个下拉/灯箱功能,以展示我的CSS知识。因此,当点击其中一个链接时,我希望它能够到达我想要的位置;另外,我希望能够反映当前哪个链接&#34;活跃&#34; (或不)通过将其state.active更改为truefalse并为CSS目的添加其他类active。我想要新的&#34;活跃&#34;类仅出现在<NavEndpt />上,state.activetrue

然而,到目前为止,我所尝试过的所有内容都没有奏效,而且我已经有两天了。我很感激有一个对React更有经验的人向我展示如何实现这一目标。

以下是我的工作内容:

var MasterLayout = React.createClass({
  mixins: [History],

  render: function(){
      var childrenWithProps = React.Children.map(this.props.children,
            (child) => React.cloneElement(child, {
                doSomething: this.doSomething
            })
          );

    return(

        <div id="container">
          <Navigation activeRoute={this.props.location.pathname} />
          <div id="content">
            {childrenWithProps}
          </div>
        </div>
      )

  }
});

var Navigation = React.createClass({
    getInitialState: function(){
      var endpoints = require("./data/naviEnd.js");
      return {
        endpoints: endpoints,
        activeRoute: this.props.activeRoute
      }           
    },
renderEndpoints: function(key){
      var endpointDetails = this.state.endpoints[key];

      return(
        <NavEndpt id={endpointDetails.id} key={endpointDetails.title} url={endpointDetails.url} title={endpointDetails.title}/>
        )

    },

    render: function(){


      return(
          <div id="navigation">
              {Object.keys(this.state.endpoints).map(this.renderEndpoints)}
          </div>
        )
      }
    });

// Created child not a this.props.child of <Navigation /> component 
        // as pointed out by @BenHare

        var NavEndpt = React.createClass({

          handleClick: function(){

            this.setState({
              active: true
            })

          },
          render: function(){

            return (
              <div onClick={this.handleClick} className="navLink" id={this.props.id}>
                <Link id={this.props.id + "-link"} to={this.props.url}>{this.props.title}</Link>
              </div>
              )
          }
        })

目前这只更改为每个<NavEndpt />创建和设置状态我试图使Stack Overflow尽可能干净。

到目前为止,我提出的最佳解决方案是使用大量的DOM选择和硬编码的if / else语句。它也没有点亮我的&#34;关于&#34;组件,因为它没有url属性。这很重要,因为我将以下解决方案绑定到整个布局组件的pathname

var MasterLayout = React.createClass({
  mixins: [History],

  render: function(){
      var childrenWithProps = React.Children.map(this.props.children,
            (child) => React.cloneElement(child, {
                doSomething: this.doSomething
            })
          );
return(

    <div id="container">
      <Navigation activeRoute={this.props.location.pathname} />
      <div id="content">
        {childrenWithProps}
      </div>
    </div>
  )

}

}); 


  // This is the parent component that sits on the side or the top depending on the
  // broswer size, contains components NavEndpt
  var Navigation = React.createClass({
    getInitialState: function(){
      var endpoints = require("./data/naviEnd.js");
      return {
        endpoints: endpoints,
        activeRoute: this.props.activeRoute
      }           
    },
// Always makes the website's initial view the home route
    componentDidMount: function(){
        var cover = document.getElementById("cover");
        var projects = document.getElementById("projects");
        var about = document.getElementById("about");

        var active = this.props.activeRoute

        this.setActive();
    },
// resets the hard coded CSS class
    resetClasses: function(){
      var active = this.props.activeRoute

      var cover = document.getElementById("cover");
      var projects = document.getElementById("projects");
      var about = document.getElementById("about");    

          cover.className = "navLink";
          projects.className = "navLink";
          about.className = "navLink";
    },
// checks pathname of <MasterLayout/>
// also somehow makes it so a refresh does not
// return you to "/"
    setActive: function(){
      var active = this.props.activeRoute

      var cover = document.getElementById("cover");
      var projects = document.getElementById("projects");
      var about = document.getElementById("about");

        if (active === "/"){

        cover.className += " active";
      } else if (active === "/projects"){

        projects.className += " active"
      } else if (active === "/about"){
        about.className += " active"
      }
},
// listens for updates, resets active first and sets it
componentDidUpdate: function(){

  this.resetClasses();
  this.setActive();

},

renderEndpoints: function(key){
  var endpointDetails = this.state.endpoints[key];

  return(
    <NavEndpt id={endpointDetails.id} key={endpointDetails.title} url={endpointDetails.url} title={endpointDetails.title}/>
    )

},

render: function(){


  return(
      <div id="navigation">
          {Object.keys(this.state.endpoints).map(this.renderEndpoints)}
      </div>
    )
  }
});



    var NavEndpt = React.createClass({

      handleClick: function(){

        this.setState({
          active: true
        })

      },
      render: function(){

        return (
          <div onClick={this.handleClick} className="navLink" id={this.props.id}>
            <Link id={this.props.id + "-link"} to={this.props.url}>{this.props.title}</Link>
          </div>
          )
      }
    })

0 个答案:

没有答案