javascript - mapStateToProps 对象未定义

标签 javascript reactjs react-redux undefined jsx

我正在调用 API 以使用其 ID 获取特定对象。

当我在 mapStateToProps 方法中调用 console.log 时,会打印整个对象,但状态的“anuncio”属性是未定义当我尝试使用 this.props.anuncio 访问它时。我做错了什么?

下面是我如何使用 reducer 和操作。

import React from 'react'
import PropTypes from 'prop-types'
import { fetchAnuncio } from '../../actions/anuncios';
import { connect } from 'react-redux';
import Avatar from 'react-avatar';
import star from '../../imgs/star.png';

class AnuncioDetalhes extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: this.props.anuncio ? this.props.anuncio.id : null,
      img: this.props.anuncio ? this.props.anuncio.img : null
    }
  }


  componentDidMount() {
    if (this.props.match.params.id) {
      this.props.fetchAnuncio(this.props.match.params.id);
    }
  }

  render () {
    const {id, img} = this.state;
    return (
      <div>
      </div>

    );
  }
}

AnuncioDetalhes.propTypes = {
  fetchAnuncio: PropTypes.func.isRequired,
  history: PropTypes.object.isRequired
}

  function mapStateToProps(state, props) {

  if(props.match.params.id) {
    console.log(state.anuncios.find(item => item.id === 4))
    return {
      anuncio: state.anuncios.find(item => item.id === props.match.params.id)
    }
  }
  return { anuncio: null };
}

export default connect(mapStateToProps, {fetchAnuncio})(AnuncioDetalhes);

reducer :

import { SET_ANUNCIOS, ANUNCIO_FETCHED } from '../actions/types';

export default function anuncios(state = [], action = {}) {
  switch(action.type) {

    case ANUNCIO_FETCHED:
      const index = state.findIndex(item => item.id === action.anuncio.id);

      if(index > -1) {
        return state.map(item => {
          if (item.id === action.anuncio.id) return action.anuncio;
          return item;
        });
      } else {
        return [
          ...state,
          action.anuncio
        ];
      }

    case SET_ANUNCIOS:
      return action.anuncios;
    default: return state;
  }
}

行动:

import axios from 'axios';
import Constants from '../components/Constants'
import { SET_ANUNCIOS, ANUNCIO_FETCHED } from './types';


export function setAnuncios(anuncios) {
  return {
    type: SET_ANUNCIOS,
    anuncios
  }
}

export function anuncioFetched(anuncio) {
  return {
    type: ANUNCIO_FETCHED,
    anuncio
  };
}

export function fetchAnuncios(cidade) {
  return dispatch => {
    return axios.get(Constants.BASE_URL + "anuncios/?user__cidade=" + cidade + "&status=2").then(res => {
      dispatch(setAnuncios(res.data.results));
    });
  }
}

export function fetchAnuncio(id) {
  return dispatch => {
    return axios.get(Constants.BASE_URL + "anuncios/" +`${id}`).then(res => {
      dispatch(anuncioFetched(res.data));
    });
  }
}

最佳答案

我认为您应该使用 componentWillReceiveProps 生命周期方法来获取更新的 Prop 。

componentWillReceiveProps = (nextProps)  => {
    console.log(nextProps)
} 

关于javascript - mapStateToProps 对象未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50235666/

相关文章:

javascript - React Router 中的 match.url 到底是什么?

javascript - 导入变量,但在使用 ReactJS Redux 时出现导入错误

javascript - 如何在 React 中集中处理 API 调用的错误(使用 Axios)

javascript - 为 javascript 创建正则表达式

javascript - 试图使下一个和上一个按钮出现在注册表单的不同时间

javascript - 在 javascript 初始化时具体化重复项

javascript - 使用 Hooks 在 React 中更新数组

javascript - 根据redux中的id将对象添加到数组

javascript - 如果已从服务器端获取数据,则阻止 componentDidMount 获取数据

javascript - 按值访问 XML 节点