如何使用React-Redux使XHR请求的响应进入状态

问题描述 投票:0回答:1

我试图在我的React容器中获得XHR请求的响应作为道具。我正在尝试做的是向api发出GET请求,并将响应转换为一个名为“ game”的对象,我可以在其中进行反应。我成功完成了与GET请求类似的操作,该请求返回了一个称为“游戏”的对象数组,可以在响应中进行映射;但是,当我尝试请求“游戏”对象时,我从api得到了很好的响应,但是react显示未定义“游戏”。

我认为我的减速器或操作文件可能存在问题,也许是如何设置我的初始状态,但我不知道如何使它工作。

感谢您提供的所有帮助!非常感谢您的时间和耐心。

我将在下面发布我的代码。

Reducer文件如下所示:

import { LOAD_GAMES, SET_CURRENT_GAME } from "../actionTypes";

const game = (state = [], action) => {
    switch (action.type) {
        case LOAD_GAMES:
            return [...action.games];
        case SET_CURRENT_GAME:
            return [...action.game];
        default:
            return state;
    }
};

export default game;

动作文件看起来像这样:

import { apiCall } from "../../services/api";
import { addError } from "./errors";
import { LOAD_GAMES, SET_CURRENT_GAME } from "../actionTypes"; 

export const loadGames = games => ({
  type: LOAD_GAMES,
  games
});

export const setCurrentGame = game => ({
    type: SET_CURRENT_GAME,
    game
});

export const fetchGames = () => {
  return dispatch => {
    return apiCall("GET", "api/games/")
      .then(res => {
        dispatch(loadGames(res));
      })
      .catch(err => {
        dispatch(addError(err.message));
      });
  };
};

//WRITE A FUNCTION TO SET_CURRENT_GAME TO BE THE ID OF THE GAME THAT IS CLICKED ON.
export const getGameDetails = game_id => {
    return dispatch => {
        return apiCall("GET", `/api/games/${game_id}`)
            .then(res => {
                dispatch(setCurrentGame(res));
        })
        .catch(err => {
            dispatch(addError(err.message));
        });
    };
};

export const postNewGame = title => (dispatch, getState) => {
  return apiCall("post", "/api/games", { title })
    .then(res => {})
    .catch(err => addError(err.message));
};

React容器看起来像这样:

import React, { Component } from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
import { getGameDetails } from "../store/actions/games";

class GameDetails extends Component {

componentDidMount() {
    const game_id= this.props.match.params.game_id;
    this.props.getGameDetails(game_id);
}
render() {
    const { game } = this.props;

    return (
        <div className="home-hero">
            <div className="offset-1 col-sm-10">
                    <h4>You are viewing the Game Page for {game.title}</h4>
            </div>
        </div>
    );
}
}

function mapStateToProps(state) {
return {
    game: state.game
    };
}

export default connect(mapStateToProps, { getGameDetails })(
    GameDetails
);

编辑-通过显示游戏响应数组的地图而工作的GameList容器如下所示:

import React, { Component } from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
import { fetchGames } from "../store/actions/games";

class GameList extends Component {
    componentDidMount() {
        this.props.fetchGames();
    }
    render() {
        const { games } = this.props;
        let gameList = games.map(g => ( 
            <li className="list-group-item" key= {g._id}>
                <Link to={`/games/${g._id}`}>
                    {g.title}
                </Link>
            </li>
        ));
        return (
            <div className="row col-sm-8">
                <div className="offset-1 col-sm-10">
                    <ul className="list-group" id="games">
                        {gameList}
                    </ul>
                </div>
            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        games: state.games
    };
}

export default connect(mapStateToProps, { fetchGames })(
    GameList
);

为澄清起见,reducer的“ LOAD_GAMES”部分和操作正常;但是,“ SET_CURRENT_GAME”不起作用。

reactjs redux react-redux store reducers
1个回答
0
投票

我认为您使用的是减速器错误

reducer的状态应该是对象,而不是数组IMO,例如这样:

import { LOAD_GAMES, SET_CURRENT_GAME } from "../actionTypes";

const initState = {
    current: undefined,
    list: []
}
const game = (state = initState, action) => {
    switch (action.type) {
        case LOAD_GAMES:
            state.list = action.games;
            return state
        case SET_CURRENT_GAME:
            state.current = action.game;
            return state;
        default:
            return state;
    }
};

export default game;

GameDetails中的>

function mapStateToProps(state) {
    return {
       game: state.game.current
    };
}

并且在同一GameList

function mapStateToProps(state) {
    return {
       list: state.game.list
    };
}
© www.soinside.com 2019 - 2024. All rights reserved.