reactjs - 从 store 访问 state 和 props
问题描述
我刚刚开始,不知道如何从商店正确访问状态和道具。减速器代码是:
import {
REQUEST_LINKS_PENDING,
REQUEST_LINKS_SUCCESS,
REQUEST_LINKS_FAILED
} from './selecttype_constants';
const initialState = {
isPending: false,
shelters: [],
error: '',
};
export const selectShelter = (state=initialState, action) => {
switch(action.type) {
case REQUEST_LINKS_PENDING:
return Object.assign({}, state, {isPending: true});
case REQUEST_LINKS_SUCCESS:
return Object.assign({}, state, {shelters: action.payload, isPending: false});
case REQUEST_LINKS_FAILED:
return Object.assign({}, state, {error: action.payload, isPending: false});
default:
return state;
}
};
组件代码是
import React from 'react';
import { connect } from 'react-redux';
const mapStateToProps = (state) => {
return {
isPending: state.isPending,
shelters: state.shelters,
}
}
class SheltersList extends React.Component {
render() {
const { isPending, shelters} = this.props;
if (isPending === false) {
return (
<div>
{ shelters.map(function(shelter) {
<option id={shelter.id}>{shelter.name}</option>
})}
</div>
)
} else {
console.log('fetching')
return (
<option id='2'> Nahravam Data... </option>
)}
}
}
export default connect(mapStateToProps, null)(SheltersList);
访问isPending
真/假状态和shelters
数组以便将其映射到组件中的正确方法是什么?在“mapStateToProps should I use
state.isPending and
state.shelters to access the property the right way or should it be
state.selectShelter.isPending and
stet.selectShelter.sheltersShelters.map ”? Cause in first case it never loads the shelters to the list and in second case I get an error I get an error that
中不是一个函数。
解决方案
推荐阅读
- elasticsearch - ElasticSearch 按数组字段过滤 不包含某些值
- c++ - 任何从 c++filt demangled 输出中获取更多信息的方式
- elasticsearch - 如何将布尔查询与间隔查询或任何其他查询结合起来
- java - Swagger Codegen (Springboot) 生成的代码的“任何”类没有反序列化
- mfc - 午餐远程应用程序 C++ MFC
- windows - OpenSSH Windows 10 强制基于密钥的登录
- vb.net - 如何纠正“无效的外部程序”错误?
- sql - 选择 Postgres 中所有主键的最后一个条目
- powershell - 在 PowerShell 中编辑(拆分)字符串?
- reactjs - 如何在 React 中实现延迟加载或滚动加载