我正在使用react-router,我需要将 Prop 传递给处理程序Comments
组件(请看下面的代码)。通常是solved by creating a wrapper component .
Nhưng Comments
Đã AbstractComments
的 es6 包装器,所以我不想再创建一个包装器。所以我用下面的方式来做:
问题/疑问:Vui lòng kiểm traComments#onChange
方法
评论组件
// 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);
}
}
react-router 如何渲染 Comments
:
var Index = React.createClass({
render: function () {
trở lại (
);
}
});
var routes = (
);
ReactRouter.run(routes, function (Handler) {
React.render(, document.body);
});
将默认值分配给defaultProps中的 Prop ,如下所示。
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 ;
Tôi là một lập trình viên xuất sắc, rất giỏi!