【发布时间】:2016-04-11 16:06:06
【问题描述】:
我正在学习 React 并遵循一些教程,我遇到了这段代码:
import React from 'react';
import TodosView from 'components/TodosView';
import TodosForm from 'components/TodosForm';
import { bindActionCreators } from 'redux';
import * as TodoActions from 'actions/TodoActions';
import { connect } from 'react-redux';
@connect(state => ({ todos: state.todos }))
export default class Home extends React.Component {
render() {
const { todos, dispatch } = this.props;
return (
<div id="todo-list">
<TodosView todos={todos}
{...bindActionCreators(TodoActions, dispatch)} />
<TodosForm
{...bindActionCreators(TodoActions, dispatch)} />
</div>
);
}
}
这是一个待办事项应用程序,这是主页,它加载了另外 2 个小的 components。我几乎了解所有内容,但我无法理解:
-
connect有什么作用?我知道你必须传递 4 个参数(不过我无法准确地知道这 4 个变量是什么)。 -
@connect装饰器的实现如何,代码转译后的样子?
提前致谢:)
【问题讨论】:
-
Redux 和 react-redux 文档在这个主题上非常完整。关于
connect为您做什么:redux.js.org/docs/basics/UsageWithReact.html,关于connect的 API 以及四个参数是什么:github.com/reactjs/react-redux/blob/master/docs/… -
嗯,我从同一个链接了解了 4 个参数,但我无法从那里正确理解,这就是为什么我在这里问一个问题 :) :)
-
好的,我会发布一个快速的答案,但我怀疑我能比文档做得更好! :)
-
我要补充一点,官方文档从不在任何地方使用装饰器,因为它是一个不稳定的特性,以后可能会改变。不要使用它,除非有一天你能接受它。
标签: reactjs redux react-redux