简体   繁体   中英

React.js : passing props to a react router linked component

I want to pass props from a component to a child component through react-router. My flow is something like this : NaturalPersonList->nav->Naturalperson . NaturalPersonList component renders a nav component, Inside my nav component , I use react-router's Link module to link to Naturalperson component. I want to pass a prop from my NaturalPersonList component down to Naturalperson , but since Naturalperson is not explicitly rendered and I'm just linking to it , I'm not able to pass the props. Is there any way I can acheive this}

NaturalPersonList :

var Nav=require('./nav.jsx');
var Router = require('react-router');
module.exports = React.createClass({


      render: function render() {

        return (<div><Nav nav1="Natural Person 1" nav2="Natural Person 2" nav3="Natural Person 3" nav4="Natural Person 4" section1="NaturalPerson" section2="NaturalPerson" section3="NaturalPerson" section4="NaturalPerson" firstName={this.props.firstName}/> 
        <Router.RouteHandler {...this.props}/></div>

        );
      }
});

Navigation component :

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=NaturalPerson params={{naturalPersonId: 1}} className={this.props.navSelection==='nav-section1'?activeClass:''} 
                    activeClassName={activeClass}>{this.props.nav1}</Link>
            </li>     
          </ul>
        </nav>
      </section>
    );
  }
});

NaturalPerson :

module.exports = React.createClass({


    getInitialState :function() {
    return{
             readOnly : true


          };
          },

          submit : function(){
            this.setState({readOnly : false});
              console.log(this.state.id);
          },
          save : function(){
            this.transitionTo('/');
          },

      render: function render() {

        return (<form action="/Output" method="POST" ><div id="container_suspects">
          <input  type="submit" value="Save" ></input>     
          <button type="button" onClick={this.submit} >Edit Details</button>
        <div id="suspects_personal_details"><PD hidden="hidden"  readOnly={this.state.readOnly} 
        firstName={this.props.firsName}/><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>
        </form>
        );
      }
});

If i correctly understand you. You can send props from Link:

<Link to=NaturalPerson params={{naturalPersonId: 1}}
      className={this.props.navSelection==='nav-section1'?activeClass:''} 
      params={params: {dictionary}}
      activeClassName={activeClass}>
    {this.props.nav1}
</Link>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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