React.js:根据传递的id以不同方式呈现相同的组件

时间:2016-04-26 09:17:23

标签: reactjs react-router

我正在使用react,我有一个名为NaturalPerson的组件,我在导航组件中使用此组件。我想基于传递的id以不同的方式呈现相同的组件(NaturalPerson)。有没有办法做到这一点?

NaturalPerson:

module.exports = React.createClass({

    getInitialState :function() {
    return{
             readOnly : true

          };
          },

          submit : function(){
            this.setState({readOnly : false});
          },
          save : function(){
            return '/';
          },

      render: function render() {

        return (<div id="container_suspects">
         <button type="button" onClick={this.submit} >Edit Details</button>
        <div id="suspects_personal_details"><PD hidden="hidden"  readOnly={this.state.readOnly}/><br/><br/>
        <PNS hidden="hidden" readOnly={this.state.readOnly}/><br/><br/>
        <Email readOnly={this.state.readOnly} /><br/><br/></div>
        <div id="suspects_address"><A  readOnly={this.state.readOnly}/><br/><br/></div>
       </div>

        );
      }
});

nav.jsx:

module.exports = React.createClass({

  displayName: 'nav',

  render: function render() {   
    var activeClass = 'left-nav-selected';

    return (
      <section className='left-nav' id='left-nav'>
        <div className='left-nav-title'>{this.props.name}</div>
        <nav className='left-nav-links'>
          <ul>
            <li className='left-nav-link' id='nav-section1'>
              <Link to={this.props.section1} params={{naturalPersonId: 1}} className={this.props.navSelection==='nav-section1'?activeClass:''} 
                    activeClassName={activeClass}>{this.props.nav1}</Link>
            </li>
            <li className='left-nav-link' id='nav-section2'>
              <Link to={this.props.section2} params={{naturalPersonId: 2}} className={this.props.navSelection==='nav-section2'?activeClass:''} 
                    activeClassName={activeClass}>{this.props.nav2}</Link>
            </li>
            <li className='left-nav-link' id='nav-section3'>
              <Link to={this.props.section3} params={{naturalPersonId: 3}} className={this.props.navSelection==='nav-section3'?activeClass:''} 
                    activeClassName={activeClass}>{this.props.nav3}</Link>
            </li>   
            <li className='left-nav-link' id='nav-section4'>
              <Link to={this.props.section4} params={{naturalPersonId: 4}} className={this.props.navSelection==='nav-section4'?activeClass:''} 
                    activeClassName={activeClass}>{this.props.nav4}</Link>
            </li>     
          </ul>
        </nav>
      </section>
    );
  }
});

routes.jsx:

<Route name='NaturalPerson' path='NaturalPerson/:naturalPersonId' handler={NaturalPerson}/>

1 个答案:

答案 0 :(得分:0)

您需要做的就是从路线中获取naturalPersonId并根据您获得的输入添加条件。这是实现这一目标的一种方式:

render: function render() {
  let id = this.props.routeParams.naturalPersonId;
  return (
    //JSX goes here...
  );
}

或者,您可以在渲染中执行条件并相应地调用不同的函数:

render: function render() {
  let id = this.props.routeParams.naturalPersonId;
  if(id == 1) {
    return this.customRenderOne();
  } else {
    return this.customRenderTwo();
  }
}