简体   繁体   中英

react-router: pass props to es6 handler component

I'm using react-router, and I need to pass props to handler Comments component (look at the code below). It's usually solved by creating a wrapper component .

BUT Comments is already a es6 wrapper of AbstractComments , so I don't want to create one more wrapper. So I do it in the next way:

Problem/Question: look please at Comments#onChange method

Comments component

// Where AbstractComment extends React.Component
class Comments extends AbstractComments {

    constructor(props) {
        //Pass my custom props to component
        super(_.assign(props, {
            chatName: '....',
            title: '...',
            comments: '.....'
        }));
    }

    _onChange() {
       //PROBLEM: after setState Comments component get rendered 
       //without my custom props: (chatName, title, comments)
       //QUESTION: how to re-render it with the same properties?
       this.setState(someNewState);
    }

}

How Comments is rendered by react-router:

var Index = React.createClass({
  render: function () {
    return (
        <div>
            <header>Some header</header>
            <RouteHandler />
        </div>
    );
  }
});

var routes = (
  <Route path="/" handler={Index}>
    <Route path="comments" handler={Comments}/>
    <DefaultRoute handler={Dashboard}/>
  </Route>
);

ReactRouter.run(routes, function (Handler) {
  React.render(<Handler/>, document.body);
});

Assign the default values to props in the defaultProps as shown below.

class Comments extends AbstractComments {

   constructor(props) {
    //Pass my custom props to component
    super(_.assign(props, {
        chatName: '....',
        title: '...',
        comments: '.....'
    }));
   }

   _onChange() {
   //PROBLEM: after setState Comments component get rendered 
   //without my custom props: (chatName, title, comments)
   //QUESTION: how to re-render it with the same properties?
   this.setState(someNewState);
  }

}
Comments.defaultProps = {chatName: '....',
        title: '...',
        comments: '.....'};

export default Comments ;

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