【发布时间】:2020-08-22 05:33:15
【问题描述】:
如何等待 react-redux 返回我的新连接组件?基本上,我有一个索引屏幕,我在其中对数据库进行查询以保存客户端,该 api 调用的响应是我想要触发我的 SAVE_CUSTOMERS 操作然后在视图中制作地图的参数。
这个错误是因为我想要制作地图的对象尚不存在,这就是为什么我伪造一个等待该哈希以及它是否有效。
我有几种方法可以强制它工作,但我来这里是为了一个奇特的解决方案????
customers.js
import React from "react";
import bridge from "../../helpers/bridge/index";
import { save_customers } from "../../../actions/customers";
import CustomersUI from "../../components/Customers/CustomersUI";
import { connect } from "react-redux";
class CustomersScreen extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
bridge
.getCustomers(this.props.user.access_token)
.then((customers) => this.props.save_customers(customers))
.catch((error) => console.error(error));
}
render() {
return (
<CustomersUI
customers={this.props.customers}
/>
);
}
}
export default connect(
(state) => ({
user: state.user,
customers: state.customers,
}),
{
save_customers: save_customers,
}
)(CustomersScreen);
我的减速器
export default (state = {}, action) => {
switch (action.type) {
case "SAVE_CUSTOMERS":
return action.customers;
default:
return state;
}
};
还有……动作,
export function save_customers(customers) {
return {
type: "SAVE_CUSTOMERS",
customers,
};
}
显然我有一个 combineReducers 导出所有内容。
也许等待组件被连接并不是解决它的最花哨的方法,它只是我对它的最接近的想法。不到 1 周前,我还在使用 React Native,之前从未使用过 Redux。谢谢!
【问题讨论】:
-
...props.customers.map...来自哪里? -
来自我的用户界面。我在哪里做
.map。我不想添加它以免听起来多余。类似于props.customer.map((customer)=> (COMPONENT...))
标签: reactjs react-native redux react-redux expo