简体   繁体   English

调用组件渲染时,Redux数据使我不确定

[英]Redux data is giving me undefined when calling on a component rendering

I am utilizing thunk with react and redux. 我在用thunk和react和redux。 My action creator executes an API call and returns data in a property "data" and my reducer returns that object. 我的动作创建者执行API调用,并在属性“数据”中返回数据,而我的reducer返回该对象。 I have this returned object mapped to props in my component. 我将此返回的对象映射到组件中的props。 Its an array of 16 items (each item is an image url). 它由16个项目组成(每个项目都是一个图片网址)。 when I console.log(this) I can click through and see the data, but if I go further like console.log(this.props.showGallery.imageLinks) it shows undefined. 当我console.log(this)时,我可以单击以查看数据,但是如果我像console.log(this.props.showGallery.imageLinks)一样走得更远,它将显示未定义。

Another situation is that when I render { this.props.showGallery.imageLinks } I can clearly see all the text of the items in the array on my web page but when I use .map on it, the console says cannot read property "map" of undefined and the web page is just empty. 另一种情况是,当我渲染{this.props.showGallery.imageLinks}时,我可以清楚地看到网页上数组中所有项目的文本,但是当我在其中使用.map时,控制台会显示无法读取属性“ map”。 ”(未定义),并且网页为空。 Am I doing this wrong? 我做错了吗? How can I make this data like normally? 我怎样才能像平常一样使这些数据?

Am I understanding redux concepts wrongly? 我是否错误地理解了redux概念?

actions .js 动作 .js

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) {
        return dispatch({ type: SHOW_GALLERY, data: [...res.data] });
      }
    })
    .catch(err => console.error(err));
  }
}

reducer images.js 减速机images.js

import { HEAD_SELECTED, SHOW_GALLERY } from '../actions/actions';
import { actionSelectHeadImg } from '../actions/actions';
import { actionGallery } from '../actions/actions';


// Show the image links from an array
export function showGallery(state={}, action) {
  switch(action.type) {
    case SHOW_GALLERY:
      return Object.assign({}, state, { imageLinks: new Array(action.data) })
    default:
      return state;
    }
  }

combined Reducers for above: 以上的组合减速器

import React from 'react';
import { combineReducers } from 'redux';
import { showGallery, headSelected } from './images';

// Combine all your reducers to this
const allReducers =  combineReducers({
  showGallery,
  headSelected
});

export default allReducers;

component / container Gallery.js 组件/容器 Gallery.js

import React from 'react';
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { actionGallery } from '../actions/actions';
import cheerio from 'cheerio';

class Gallery extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: []
    }
    this.props.actionGallery();
  }
  render() {
    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.showGallery.imageLinks.forEach((i, index) => {
                  <p>i</p>
                }) }
              </div>
          </div>
        </div>
      </div>
    );
  }
}



function mapStateToProps(state) {
  return {
    showGallery: state.showGallery,
    headSelected: state.headSelected,
    newState: state
  }
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({
    actionGallery,
    actionSelectHeadImg
  }, dispatch)
}


export default connect(mapStateToProps, mapDispatchToProps)(Gallery);

A regular component that just holds the other container/components 一个仅容纳其他容器/组件的常规组件

import React from 'react';
import ReactDOM from 'react-dom';
import Gallery from './containers/Gallery';
import HeadingImg from './containers/HeadingImg';

class App extends React.Component {
  render() {
    //console.log(this.props.actionGallery())
    return (
      <div>
        <center>
          <h3>SELECTED IMAGE:</h3>
          <br />
          <HeadingImg />
          <hr />
          <Gallery />
        </center>
      </div>
    );
  }
}

export default App;

TOP LEVEL COMPONENT (MAIN COMPONENT) 顶级组件(主要组件)

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import App from '../Components/Earth/app';
import allReducers from '../Components/Earth/reducers';
import thunk from 'redux-thunk';



const store = createStore(allReducers, applyMiddleware(thunk));


ReactDOM.render(
  <Provider store={store}>
    <App store={store}/>
  </Provider>
, document.getElementById("root"));

I'm assuming when you create your store, you only have the one reducer. 我假设当您创建商店时,您只有一个减速器。 If that is the case then your assumption about 'state.showGallery' existing doesn't. 如果真是这样,那么您关于'state.showGallery'存在的假设就不会成立。 Instead imageLinks will be in state without the 'showGallery'. 相反,imageLink将处于没有“ showGallery”状态。

If my assumption is correct, then you should change your mapStateToProps to have showGallery as: 如果我的假设是正确的,那么您应该将mapStateToProps更改为showGallery为:

showGallery: { imageLinks: state.imageLinks },

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

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