React.js不区分同一类的两个组件
React.js does not differentiate between two components of same class
不确定这是React.js错误还是Javascript语言的"特性"。我有一个名为Page
的React.js(编辑:使用v0.12.x)组件,它充当路由器并根据路由选择子组件。我创建了一个mixin,它添加了一个名为runRoute
的函数,该函数采用JSX组件,并使用Page
的setState
方法来更新state.routeComponent
。此外,runRoute
还调用forceUpdate()
以确保Page
被重新渲染。
问题是,当两个组件属于同一类时,React似乎不会区分状态中的两个组件。我来解释。
我有两个子组件,称为Board
和Profile
。CCD_ 11被重新用于不同的上下文。如果两个连续的路由都使用<Board/>
,React.js不会更新UI。只有当使用不同类(如<Profile/>
)的连续路由时,UI才会真正更新。如果状态改变为另一个以不同方式使用<Board/>
的路由,则它将仅将UI更新为<Board/>
的适当状态。
下面是<Page/>
和我的Router
mixin:的一些示例代码
Router.js
module.exports = function(){
return {
getInitialState: function () {
return {
routeComponent: null
};
},
runRoute: function(component){
this.setState({routeComponent: component});
this.forceUpdate();
},
componentWillMount: function() {
var self = this;
self.router = Router(self.routes).configure({
resource: self,
notFound: self.notFound
});
self.router.init();
}
};
}
Page.jsx
module.exports = function(React, Router, NotFound, Profile, Board) {
return React.createClass({
mixins: [Router],
routes: {
'/board': 'trending',
'/board/mostloved': 'mostLoved',
'/profile': 'profile'
},
trending: function() {
this.runRoute(
<Board title="Trending Boards" setTitle={this.props.setTitle} />
);
},
mostLoved: function() {
this.runRoute(
<Board title="Loved Boards" setTitle={this.props.setTitle} />
);
},
profile: function() {
this.runRoute(
<Profile setTitle={this.props.setTitle} />
);
},
notFound: function () {
this.runRoute(
<NotFound setTitle={this.props.setTitle} />
);
},
render: function() {
return this.state.routeComponent;
}
});
};
如果有人对此有所了解,请不胜感激。谢谢
如果不查看Board
组件,就不能确定,但问题似乎是它的状态取决于属性,并且在更新属性时(在runRoute
之后)无法保持它们同步。
这里有一个简单的例子(jsfiddle)来演示这个问题:
var Board = React.createClass({
getInitialState: function() {
return {title: this.props.title}
},
render: function() {
return <h1>{this.state.title}</h1>
}
});
var Page = React.createClass({
getInitialState: function () {
return {routeComponent: null}
},
handleClick: function(title) {
var self = this;
return function() {
self.setState({ routeComponent: <Board title={title} /> })
}
},
render: function() {
return <div>
<a href='#' onClick={this.handleClick('Trending')}>Trending</a>
<br/>
<a href='#' onClick={this.handleClick('Most Loved')}>Most Loved</a>
{this.state.routeComponent}
</div>;
}
});
单击链接会更改Page
的状态,但不会更改Board
的视觉表示。
您需要通过处理componentWillReceiveProps
中的更改来保持Board
的状态与其更新的属性同步。
只需在Board
中添加几行即可解决问题:
componentWillReceiveProps: function(props) {
this.setState({title: props.title})
},
附带说明:您永远不需要this.setState()
之后立即使用this.forceUpdate()
。如果没有它就无法获得所需的行为,那就是你做错了什么。
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- React组件等待所需道具进行渲染
- 如何创建带有插槽的vue js组件预加载程序
- KnockoutJS-组件-多个实例
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何在react js中将值从一个组件发送到另一个组件
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- React组件-设置页面标题
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 如何在salesforce aura闪电组件中使用jQuery.val()
- 为react组件传递道具的最佳方式
- [Vue warn]:未能解析组件
- 如何控制组件'的createContent函数被激发
- react组件中的绑定方法
- 如何在react js中移动第二个组件
- 多个组件是如何实现的
- 组件生命周期问题/无法处理未定义的问题
- React.js不区分同一类的两个组件