拿走,例如Flux TodoMVC并且假设我想要彼此相邻的两个Todo-Apps。
class TwoTodos extends React.Component {
render () {
return (
<div>
<TodoApp />
<TodoApp />
</div>
);
}
}
现在,当你运行这个例子时,你会注意到两个Todo列表 将同步发出并听取相同的动作。
防止这种情况的规范方法是什么?
答案 0 :(得分:0)
我刚刚解决了这个问题。我花了几天时间搞清楚这一点。
通常,您应该将您的操作和存储设置为类,而不是可以在Todo组件之间共享的普通对象。然后在每个Todo组件中创建一个动作类的实例并存储类。
为了避免组件相互影响,您需要将可以由不同组件实例共享的公共变量(如TodoStore.js中的_todos)封装到您的商店类中。
然后你需要将app.js中呈现的内容包装到一个类中,并在使用它之前创建该类的实例。
我将把密钥更改放在下面的代码中。
TodoActions.js:
var Actions = function(){
//if you have any shared variables, just define them here, instead of outside of the class
this.getAction = function(){
return TodoActions;
}
var TodoActions = {...};
...
}
module.exports = Actions;
TodoStore.js:
//private functions
function create(text, todos){...}
function update(id, updates, todos){...}
var Store = function(){
var _todos = {};
this.getStore = function(){
return TodoStore;
}
var TodoStore = assign({}, EventEmitter.prototype, {...});
};
module.exports = Store;
TodoApp.react.js:
var TodoApp = React.createClass({
Store: function(){},
Actions: function(){},
componentWillMount: function(){
var store = require('path/to/TodoStore.js');
var storeInstance = new store();
this.Store = storeInstance.getStore();
var action = require('path/to/TodoActions.js');
var actionInstance = new action();
this.Store = actionInstance .getAction();
this.Store.addChangeListener(...);
}
//If you need to call methods in Actions, just call this.Actions.<method_name>
});
module.exports = TodoApp;
app.js:
var TodoApp = require('./components/TodoApp.react');
window.Todo = function(){
var todo = null; //In case you need to get/set the component
this.use = function(elementId){
todo = ReactDom.render(
<TodoApp />,
document.getElementById(elementId)
)
}
};
的index.html:
<section id="todoapp1"></section>
<section id="todoapp2"></section>
<script>
var todo1 = new Todo();
var todo2 = new Todo();
todo1.use('todoapp1');
todo2.use('todoapp2');
</script>