我不能为我的生活弄清楚我做错了什么。我检查了其他人的帖子,与我的相似,但找不到什么错误,有人可以帮忙吗?我昨天才开始做出反应和函数式编程,所以很多这些东西对我来说都是新的。据我所知,我的行为或我如何定义我的类型是正确的?这是我的代码。
index.android.js
function configureStore(initialState) {
const enhancer = compose(
applyMiddleware(
thunkMiddleWare,
loggerMiddleWare,
),
);
return createStore(reducer, initialState, enhancer)
}
const store = configureStore({});
const App = () => (
<Provider store={store}>
<AppContainer/>
</Provider>
)
AppRegistry.registerComponent('BHPApp', () => App);
以及我的组件,缩减器,操作和类型。
操作
import * as UserActions from './users'
export const ActionCreators = Object.assign({},
UserActions
);
export const SET_ALL_USERS = 'SET_ALL_USERS';
import * as types from './types'
import ApiUtils from '../lib/apiUtils'
export function fetchAllUsers() {
return (dispatch, getState) => {
return fetch(URL + '/user/getAll')
.then(ApiUtils.checkStatus)
.then(response => {
dispatch(setAllUsers({ hBaseUsers: response }));
})
.catch(e => {
console.log(e)
})
}
}
function setAllUsers( {hBaseUsers} ) {
hBaseUsers.forEach((hBaseUser) => {
console.log(hBaseUser);
});
return {
types: types.SET_ALL_USERS,
hBaseUsers,
}
}
REDUCERS
import { combineReducers } from 'redux'
import * as userReducer from './users'
export default combineReducers(Object.assign(
userReducer,
));
import createReducer from '../lib/createReducer'
import * as types from '../actions/types'
export const getUsers = createReducer({}, {
[types.SET_ALL_USERS](state, action){
let newState = {};
action.hBaseUsers.forEach( (hBaseUser) => {
newState[hBaseUser.personUniqueID] = hBaseUser;
});
return newState;
}
});
export const userCount = createReducer({}, {
[types.SET_ALL_USERS](state, action){
action.hBaseUsers.length;
}
});
CONTAINERS
class AppContainer extends Component{
render(){
return <Home {...this.props} />
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(ActionCreators, dispatch);
}
export default connect((state) => { return {} }, mapDispatchToProps)(AppContainer);
import React, { Component } from 'react'
import { connect } from 'react-redux'
import {
ScrollView,
View,
TextInput,
Image,
TouchableHighlight,
StyleSheet,
Text
} from 'react-native'
class Home extends Component{
searchPressed(){
this.props.fetchAllUsers();
}
hBaseUsers(){
return Object.keys(this.props.getUsers).map( key => this.props.getUsers[key])
}
render(){
console.log(this.hBaseUsers());
return<View>
<View>
<TouchableHighlight onPress={() => this.searchPressed() } >
<Text>Fetch Users</Text>
</TouchableHighlight>
</View>
<ScrollView>
{this.hBaseUsers().map((hBaseUser) => {
return <View>
<Text>
hBaseUser.personUniqueID
</Text>
</View>
})}
</ScrollView>
</View>
}
}
function mapStateToProps(state) {
return{
getUsers: state.getUsers
}
}
export default connect(mapStateToProps)(Home);
在我阅读的所有内容之后,我的问题可以出现在任何这些事情中,但我似乎无法找到它。据我所知,我拼写并正确定义了一切。有人可以帮帮我吗?
答案 0 :(得分:1)
您的setAllUsers操作返回的对象具有一组错误的属性:
function setAllUsers( {hBaseUsers} ) {
return {
types: types.SET_ALL_USERS,
hBaseUsers,
}
}
在return语句中,将“类型”更改为“类型”以解决此错误。
答案 1 :(得分:0)
我有同样的错误。就我而言,我错过了connect函数中用于将调度映射到props的参数“ mapDispatchToProps”。