Redux 我是否可以始终使用this.props从操作调用函数?

Redux 我是否可以始终使用this.props从操作调用函数?,redux,react-redux,Redux,React Redux,我正在做一个Redux教程,但不理解其中的某些内容。我有以下容器: import React, { Component } from 'react'; import CommentsList from "./comments_list"; import { connect } from 'react-redux'; import * as actions from '../actions'; class CommentBox extends Component { constructo

我正在做一个Redux教程,但不理解其中的某些内容。我有以下容器:

import React, { Component } from 'react';
import CommentsList from "./comments_list";
import { connect } from 'react-redux';
import * as actions from '../actions';

class CommentBox extends Component {
    constructor(props) {
        super(props);
        this.state = { comment: '' };
    }
    handleChange(event) {
        this.setState({ comment: event.target.value })
    }

    submitButton(e) {
        e.preventDefault();
        this.props.saveComment(this.state.comment);
        this.setState({ comment: '' });
    }

    render () {
        return(
            <div>
                <form onSubmit={(e) => this.submitButton(e)} className="comment-box">
                    <textarea
                        value={this.state.comment}
                        onChange={(e) => this.handleChange(e)} />
                    <button action="submit">Submit</button>
                </form>
                <CommentsList comment={this.state.comment}/>
            </div>
        );
    }
}

export default connect(null, actions)(CommentBox);
在文件的底部:

export default connect(null, actions)(CommentBox);
我习惯于使用mapStateToProps和mapDispatchToProps,但这里只导入操作,然后在submitButton(e)方法中使用:

saveComment来自actions/index.js文件:

import { SAVE_COMMENT } from './types';

export function saveComment(comment) {
    return {
        type: SAVE_COMMENT,
        payload: comment
    }
}
我是否可以始终使用this.props从actions/index.js文件调用函数?为什么不先使用MapStateTops?

我是否可以始终使用this.props从actions/index.js文件调用函数

对。从:

[mapDispatchToProps(dispatch,[ownProps]):dispatchProps]
(对象或函数):如果传递了一个对象,则假定其中的每个函数都是Redux操作创建者。一个具有相同函数名的对象,但每个动作创建者都封装在一个分派调用中,因此可以直接调用它们,将合并到组件的props中

connect
正在使用
dispatch
调用为您包装从
actions/index.js
导出的内容


为什么我不需要先使用MapStateTops

因为
MapStateTrops
mapDispatchToProps
用于不同的目的,并在合并到一起并注入组件之前分别映射

如果返回
undefined
null
,则忽略它们。在
mapstatetops
的情况下,这也意味着组件不会从存储订阅更新。同样,从以下方面:

如果不想订阅存储更新,请传递null或undefined以代替mapStateToProps


我认为这个示例中有一个错误,您的
saveComment
函数没有被调度。这是故意的吗?
this.props.saveComment(this.state.comment);
import { SAVE_COMMENT } from './types';

export function saveComment(comment) {
    return {
        type: SAVE_COMMENT,
        payload: comment
    }
}