Reactjs 如何解决reducer内部的解析错误问题?
我正在尝试将PixaBay克隆应用程序重新配置为Redux。应用程序将searchText状态设置为输入值,然后在用户键入搜索文本时触发Axios GET请求回调,然后检索图像 我在我的reducer上收到了一个解析错误,但我不理解这个错误,因为我相信代码是正确的。有人能帮我解决这个问题吗?谢谢大家! 容器Reactjs 如何解决reducer内部的解析错误问题?,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我正在尝试将PixaBay克隆应用程序重新配置为Redux。应用程序将searchText状态设置为输入值,然后在用户键入搜索文本时触发Axios GET请求回调,然后检索图像 我在我的reducer上收到了一个解析错误,但我不理解这个错误,因为我相信代码是正确的。有人能帮我解决这个问题吗?谢谢大家! 容器 import React, { Component } from 'react'; import { fetchPhotos } from '../actions/actions'; imp
import React, { Component } from 'react';
import { fetchPhotos } from '../actions/actions';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import TextField from 'material-ui/TextField';
import ImageResults from '../imageResults/ImageResults';
class Search extends Component {
FetchPhotosHandler = (e) => {
this.props.fetchPhotos(e);
}
render() {
console.log(this.props.images);
return (
<div>
<TextField
name="searchText"
value={this.props.searchText}
onChange={this.FetchPhotosHandler}
floatingLabelText="Search for photos"
fullWidth={true} />
<br />
{this.props.images.length > 0 ? (<ImageResults images={this.props.images} />) : null}
</div>
);
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ fetchPhotos, dispatch});
}
export default connect(null, mapDispatchToProps)(Search);
减速器
import { FETCH_PHOTOS } from '../actions/actions';
const initialState = {
searchText: '',
images: []
}
const reducer = (state = initialState, action) => {
switch(action.type) {
case FETCH_PHOTOS:
return {
...state,
action.data.hits
};
default:
return state;
}
}
export default reducer;
错误
您正在返回一个对象,因此需要为API响应分配一个键
return {
...state,
images: action.data.hits,
};
您正在返回一个对象,因此需要为API响应分配一个键
return {
...state,
images: action.data.hits,
};
OP在状态下使用
图像
,但无所谓,解决方案是正确的。OP在状态下使用图像
,但无所谓,解决方案是正确的。
return {
...state,
images: action.data.hits,
};