[英]Is it ok to convert Immutable.js Map to Object inside of react component
我在我的应用程序中使用了redux和Immutable.js,在我开始开发项目几周后我在应用程序中添加了Immutable,因为我不想将我当前的组件更改为Immutable的.get()语法我转换了在我的商店中使用不可变的地图,然后在我的组件中使用它们。 例:
@connect((store) => {
return {
login: store.login.toObject(),
user: store.user.toObject()
};
})
这是一种不好的做法吗?这与我的应用程序的性能有关吗?
是的,这是一个非常糟糕的做法。 为了让Immutable将您的不可变地图转换为JavaScript,它必须遍历整个对象; 这会很慢。 此外,您正在失去使用Reutable和Redux使用ImmutableJS的大量实际价值。 具体来说,您现在不能再使用shouldComponentUpdate
的简单实现来短路不必要的重新渲染。
如果您想使用ImmutableJS Maps
,但又不想使用get
语法,则可以使用Records
。 它们具有Maps
所有功能,除了任意值键(访问器语法无论如何都不起作用)。
一个简短的例子:
// Specify the allowed fields and their default values
const TodoListState = Immutable.Record({ todos: undefined, filter: undefined })
// Instantiate the record using `new` and an object with the desired KV mapping
const initialTodoState = new TodoListState({
todos: Immutable.List(),
filter: undefined, // this is already the default, but I prefer being explicit
})
const initialTodos = initialTodoState.todos // List()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.