Javascript 在redux中,我的reducer在控制台中不返回任何结果
我正在使用redux和reactjs,并在两者之间应用中间件“thunk”。在action creator中,我运行一个http请求,将响应作为对象发送给reducer,然后reducer返回该数据 我试图在控制台(或者更重要的是在首页显示的组件)中显示这些数据,但我似乎做得不对。代码如下: 动作创造者 actions.jsJavascript 在redux中,我的reducer在控制台中不返回任何结果,javascript,reactjs,redux,Javascript,Reactjs,Redux,我正在使用redux和reactjs,并在两者之间应用中间件“thunk”。在action creator中,我运行一个http请求,将响应作为对象发送给reducer,然后reducer返回该数据 我试图在控制台(或者更重要的是在首页显示的组件)中显示这些数据,但我似乎做得不对。代码如下: 动作创造者 actions.js export const SHOW_GALLERY = 'SHOW_GALLERY'; import axios from 'axios'; // FETCH THE I
export const SHOW_GALLERY = 'SHOW_GALLERY';
import axios from 'axios';
// FETCH THE IMAGES
export const actionGallery = () => {
return ( dispatch ) => {
axios.get('../images')
.then(res => {
if (res.status === 200) {
dispatch(showGalleryAsync(res.data));
}
})
.catch(err => console.error(err));
}
}
function showGalleryAsync(images) {
return {
type: SHOW_GALLERY,
payload: images
}
}
减速器
images.js
import { HEAD_SELECTED, SHOW_GALLERY } from '../actions/actions';
export function showGallery(state=[], action) {
switch(action.type) {
case SHOW_GALLERY:
let images = action.data;
let arr = [];
action.payload.map((i, index) => {
arr.push(i);
});
return arr;
default:
return state;
}
}
组成部分
Gallery.js
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { actionGallery } from '../actions/actions';
class Gallery extends React.Component {
render() {
console.log(this.props);
return (
<div>
<h1>This is the Gallery.</h1>
<br />
<div className="container">
<div className="row">
<div className="col-md-8">
<h2>H2 h2 h2 2h2 </h2>
{ this.props.actionGallery.map((link, index) => {
return <img src={link}></img>
}) }
</div>
</div>
</div>
</div>
);
}
}
function mapStateToProps(state) {
return {
showGallery: state.showGallery
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({
actionGallery: actionGallery
}, dispatch)
}
export default connect(mapStateToProps, mapDispatchToProps)(Gallery);
从'react redux'导入{connect};
从“redux”导入{bindActionCreators};
从“../actions/actions”导入{actionGallery};
类库扩展了React.Component{
render(){
console.log(this.props);
返回(
这是画廊。
H2H22H2
{this.props.actionGallery.map((链接,索引)=>{
返回
}) }
);
}
}
函数MapStateTops(状态){
返回{
展厅:state.showGallery
}
}
功能图DispatchToprops(调度){
返回bindActionCreators({
动作画廊:动作画廊
},调度)
}
导出默认连接(mapStateToProps、mapDispatchToProps)(图库);
我看不到您要将actionGallery
分派到哪里来执行http请求?我试图将其放入内部返回函数中的action creator中。我认为您没有调用您的操作。axios似乎返回了一个承诺:this.props.actionGallery需要解决该承诺,但您正在调用.map。@Chris我的意思是您在哪里调用actionGallery
(或者更具体地说this.props.dispatch(actionGallery())
在您的组件中?它是在gallery组件中还是一个HOC?actionGallery中的内部函数不向外部函数返回任何内容以返回承诺,因此您不能使用承诺链。我以前没有使用axious,但我假设这是一个异步函数?如果是,请在axio前面放置一个返回
例如,返回axios.get(“…”)…