javascript - 对 REDUX 动作和减速器感到困惑
问题描述
所以我试图从我之前的问题中重构一些代码:
所以我开始深入挖掘现有的代码模板,看看它是如何实现的。
我找到了一个 reducers.js,我在其中添加了一个新的 reducer:ActiveTenant
import Auth from './auth/reducer';
import App from './app/reducer';
import ThemeSwitcher from './themeSwitcher/reducer';
import LanguageSwitcher from './languageSwitcher/reducer';
import ActiveTenant from './activetenant/reducer';
export default {
Auth,
App,
LanguageSwitcher,
ThemeSwitcher,
ActiveTenant
};
那个新的减速器是这样的:
import { Map } from 'immutable';
import actions from './actions';
import { adalApiFetch } from '../../adalConfig';
const initState = new Map({
tenantId: ''
});
export default function(state = initState, action) {
switch (action.type) {
case actions.SET_TENANT_ACTIVE:
{
const options = {
method: 'post'
};
adalApiFetch(fetch, "/Tenant/SetTenantActive?TenantName="+state.tenantId, options)
.then(response =>{
if(response.status === 200){
console.log("Tenant activated");
}else{
throw "error";
}
})
.catch(error => {
console.error(error);
});
return state.set('tenant', state.Name);
}
default:
return state;
}
}
以及该减速器的操作
const actions = {
SET_TENANT_ACTIVE: 'SET_TENANT_ACTIVE',
setTenantActive: () => ({
type: actions.SET_TENANT_ACTIVE
}),
};
export default actions;
然后从组件本身,我需要在前端选择一行时调用动作,所以我将注释代码重构为一行。
import React, { Component } from 'react';
import { Table, Radio} from 'antd';
import { adalApiFetch } from '../../adalConfig';
import Notification from '../../components/notification';
import actions from '../../redux/activetenant/actions';
const { setTenantActive } = actions;
class ListTenants extends Component {
constructor(props) {
super(props);
this.state = {
data: []
};
}
fetchData = () => {
adalApiFetch(fetch, "/Tenant", {})
.then(response => response.json())
.then(responseJson => {
if (!this.isCancelled) {
const results= responseJson.map(row => ({
key: row.id,
TestSiteCollectionUrl: row.TestSiteCollectionUrl,
TenantName: row.TenantName,
Email: row.Email
}))
this.setState({ data: results });
}
})
.catch(error => {
console.error(error);
});
};
componentDidMount(){
this.fetchData();
}
render() {
const columns = [
{
title: 'TenantName',
dataIndex: 'TenantName',
key: 'TenantName',
},
{
title: 'TestSiteCollectionUrl',
dataIndex: 'TestSiteCollectionUrl',
key: 'TestSiteCollectionUrl',
},
{
title: 'Email',
dataIndex: 'Email',
key: 'Email',
}
];
// rowSelection object indicates the need for row selection
const rowSelection = {
onChange: (selectedRowKeys, selectedRows) => {
if(selectedRows[0].TenantName != undefined){
console.log(selectedRows[0].TenantName);
const options = {
method: 'post'
};
setTenantActive(selectedRows[0].TenantName);
/* adalApiFetch(fetch, "/Tenant/SetTenantActive?TenantName="+selectedRows[0].TenantName.toString(), options)
.then(response =>{
if(response.status === 200){
Notification(
'success',
'Tenant set to active',
''
);
}else{
throw "error";
}
})
.catch(error => {
Notification(
'error',
'Tenant not activated',
error
);
console.error(error);
}); */
}
},
getCheckboxProps: record => ({
type: Radio
}),
};
return (
<Table rowSelection={rowSelection} columns={columns} dataSource={this.state.data} />
);
}
}
export default ListTenants;
但是,我不清楚动作和减速器之间的关系,如果我检查调试器,动作被执行,并且没有收到任何参数,但减速器永远不会执行。
我必须在某处发送调度吗?我在这个谜题中缺少什么?
解决方案
您错误地使用了减速器。减速器应该是纯的。你的副作用表明你还没有理解 Redux。
与其为你写下一个解决方案(无论如何这将花费很长时间,因为你必须完全解释 Redux),我建议你花 3 个小时阅读 Redux 文档并按照教程进行操作(它们很棒)。
之后你可能想要查看Redux Thunk。但是,您可能不需要 thunks。
PS:(小事要提,但我没有看到有人Map
在 Redux 中使用 s。你这样做有什么原因吗?你可能想使用普通对象。)
推荐阅读
- amazon-dynamodb - cloudformation 模板使用 dynamodb 资源中 GSI 的新非关键属性进行更新,而无需删除 GSI 并将其重新添加回来
- networking - 在 VM 之外无法访问控制中心
- ios - FFmpeg 的音频替代品 - Core Audio IOS
- flutter - 屏幕锁定时如何保持颤动应用程序在后台运行
- javascript - 为什么在 JavaScript 中收到我的个人推文 ID 号时,最后几位数字变为“0”?
- python - 为什么将价值放在 GPU 上会减慢计算速度?
- javascript - 如何使用javascript在段落上应用选定的边框样式
- python - 检查数字是否为素数,Python抛出异常
- c# - Entity Framework Core:使用自动生成的外键作为复合主键的一部分
- python - 在 python azure web 应用程序中将数千个 blob 文件从容器移动到另一个容器的最快方法?