简体   繁体   English

React redux 未从 API 获取数据

[英]React redux not fetching data from API

Hi im new to redux and im trying to create a movie app using the API from www.themoviedb.org .嗨,我是 redux 的新手,我正在尝试使用来自www.themoviedb.org的 API 创建电影应用程序。 I am trying to display the popular movies and im sure the API link works since ive tested it in postman but i cant seem to figure out why redux doesnt pick up the data.我正在尝试显示流行的电影,并且我确定 API 链接有效,因为我在 postman 中对其进行了测试,但我似乎无法弄清楚为什么 redux 没有获取数据。

//action
import { FETCH_POPULAR } from "./types";
import axios from "axios";

export const fetchPopularMovies = () => (dispatch) => {
  axios
    .get(
      `https://api.themoviedb.org/3/movie/popular?api_key=${API}&language=en-US`
    )
    .then((response) =>
      dispatch({
        type: FETCH_POPULAR,
        payload: response.data
      })
    )
    .catch((err) => console.log(err));
};

//reducer
import { FETCH_POPULAR } from "../actions/types";

const initialState = {
  popular: [],
};

export default function (state = initialState, action) {
  switch (action.type) {
    case FETCH_POPULAR:
      return {
        ...state,
        popular: action.payload,
      };
    default:
      return state;
  }
}

import React from "react";
import { connect } from "react-redux";
import Popular from "./Popular";


const FetchedPopular = (props) => {
  const { popular } = props;
  let content = "";

  content =
    popular.length > 0
      ? popular.map((item, index) => (
          <Popular key={index} popular={item} />
        
        ))
      : null;

  return <div className="fetched-movies">{content}</div>;
};

const mapStateToProps = (state) => ({
  popular: state.popular.popular,
});

export default connect(mapStateToProps)(FetchedPopular);

import React from "react";
import "../Styles.css";

const Popular = (props) => {
  return (
    <div className="movie-container">
      <img
        className="poster"
        src={`https://image.tmdb.org/t/p/w400/${props.poster_path}`}
      />
      
    </div>
  );
};

export default Popular;

I cant really tell what I'm missing can someone help?我真的不能告诉我错过了什么有人可以帮忙吗?

Next to mapStateToProps you need to create mapDispatchToProps .mapStateToProps旁边,您需要创建mapDispatchToProps After that, you will be able to call your Redux action from your React component.之后,您将能够从您的 React 组件调用您的 Redux 操作。 I suggest you the mapDispatchToProps as an Object form.我建议您将mapDispatchToProps 作为 Object表格。 Then you need to use this mapDispatchToProps as the second parameter of your connect method.然后你需要使用这个mapDispatchToProps作为你的connect方法的第二个参数。

When you will have your action mapped to your component, you need to call it somewhere.当你将你的动作映射到你的组件时,你需要在某个地方调用它。 It is recommended to do it for example on a component mount.建议例如在组件安装上执行此操作。 As your React components are Functional components, you need to do it in React useEffect hook .由于您的 React 组件是函数式组件,因此您需要在React useEffect hook中进行操作。

import React, { useEffect } from "react";
import { connect } from "react-redux";
import Popular from "./Popular";
import { fetchPopularMovies } from 'path_to_your_actions_file'

const FetchedPopular = (props) => {
  const { popular } = props;
  let content = "";

  useEffect(()=> {
    // call your mapped action (here it is called once on component mount due the empty dependency array of useEffect hook)
    props.fetchPopularMovies();
  }, [])

  content =
    popular.length > 0
      ? popular.map((item, index) => (
          <Popular key={index} popular={item} />
        
        ))
      : null;

  return <div className="fetched-movies">{content}</div>;
};

const mapStateToProps = (state) => ({
  popular: state.popular.popular,
});

// create mapDispatchToProps
const mapDispatchToProps = {
  fetchPopularMovies
}

// use mapDispatchToProps as the second parameter of your `connect` method.
export default connect(mapStateToProps, mapDispatchToProps)(FetchedPopular);

Moreover, as I wrote above in my comment, your Popular does not have the prop poster_path but it has the prop popular which probably has the property poster_path .此外,正如我在上面的评论中所写,您的Popular没有 prop poster_path但它具有可能具有属性poster_path的 prop popular

import React from "react";
import "../Styles.css";

const Popular = (props) => {
  return (
    <div className="movie-container">
      <img
        className="poster"
        src={`https://image.tmdb.org/t/p/w400/${props.popular.poster_path}`}
      />
      
    </div>
  );
};

export default Popular;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM