gpt4 book ai didi

javascript - 如何使用react-redux将ReactJS组件正确连接到Redux?

转载 作者:行者123 更新时间:2023-11-28 18:08:02 24 4
gpt4 key购买 nike

我正在尝试在 React 组件与 Redux 之间建立第一个连接,以从我的节点 API 收集数据。

目前这是一个简单的组件,但它将来会增长,并将派生出子组件,使我能够构建完整的用户 CRUD 界面(列表、编辑、删除、查看等)。从这个意义上说,我需要将操作函数连接到对象 this.props ,以便子组件可以继承它。

这是我的代码:

组件 UserGrid - 将加载用户信息的表格数据网格

import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as userActions from '../../actions/userActions';

class UserGrid extends React.Component {
componentWillMount() {
this.props.fetchUsers();
}

render() {
const mappedUsers = users.map(user => <li>{user.email}</li>);
return (
<div>
<ul>{mappedUsers}</ul>
</div>
);
}
}

function mapStateToProps(state) {
return {
users: state.users
}
}

export default connect(
mapStateToProps,
bindActionCreators(userActions, dispatch)
)(UserGrid);

我的userAction,它将有效地从 AJAX 调用加载用户:

import axios from "axios";

export function fetchUsers() {
return function(dispatch) {
axios.get("/api/users")
.then((response) => {
dispatch({type: "FETCH_USERS_FULFILLED", payload: response.data});
})
.catch((err) => {
dispatch({type: "FETCH_USERS_REJECTED", payload: err});
})
}
}

使用此代码我收到以下错误:

Uncaught ReferenceError: dispatch is not defined
at Object.<anonymous> (bundle.js:37984)
at __webpack_require__ (bundle.js:556)
at fn (bundle.js:87)
at Object.<anonymous> (bundle.js:37711)
at __webpack_require__ (bundle.js:556)
at fn (bundle.js:87)
at Object.<anonymous> (bundle.js:8466)
at __webpack_require__ (bundle.js:556)
at fn (bundle.js:87)
at Object.<anonymous> (bundle.js:588)
(anonymous) @ bundle.js:37984
__webpack_require__ @ bundle.js:556
fn @ bundle.js:87
(anonymous) @ bundle.js:37711
__webpack_require__ @ bundle.js:556
fn @ bundle.js:87
(anonymous) @ bundle.js:8466
__webpack_require__ @ bundle.js:556
fn @ bundle.js:87
(anonymous) @ bundle.js:588
__webpack_require__ @ bundle.js:556
(anonymous) @ bundle.js:579
(anonymous) @ bundle.js:582

将我的组件连接到创建的 Redux 服务的正确方法是什么?

最佳答案

在您想要连接的组件中,您需要定义函数mapDispatchToProps。该函数会将dispatch作为参数,当connect调用该函数时会接收到这个参数。

这是从我最近的一个项目中提取的一些示例代码。

   function mapDispatchToProps(dispatch) {
return {
adminActions: bindActionCreators(adminOrdersActions, dispatch),
schoolActions: bindActionCreators(schoolOrdersListActions, dispatch),
userActions: bindActionCreators(userActions, dispatch)
};
}

export default connect(mapStateToProps, mapDispatchToProps)(AllOrders);

现在调度已定义。

编辑:为了澄清,这样做现在可以让您使用以下语法访问您的操作。 this.props.actions.yourAction,或者您在 mapDispatchToProps 中调用的操作。

这是我更改后的代码:

import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

import * as userActions from '../../actions/userActions';

class UserGrid extends React.Component {

componentWillMount() {
console.log(JSON.stringify(this.props));

//this.props.fetchUsers();
this.props.actions.fetchUsers();
}

render() {

const mappedUsers = users.map(user => <li>{user.email}</li>)

return (
<div>
<ul>{mappedUsers}</ul>
</div>
)
}
}

function mapStateToProps(state) {
return {
users: state.users
}
}

function mapDispatchToProps(dispatch) {
// this function will now give you access to all your useractions by simply calling this.props.actions.
//if the key of this object would not be actions, but rather foobar, you will get your actions by
// calling this.props.foobar
return {
actions: bindActionCreators(userActions, dispatch)
}
}

export default connect(
mapStateToProps,
mapDispatchToProps
)(UserGrid);

关于javascript - 如何使用react-redux将ReactJS组件正确连接到Redux?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42209268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com