0

I can't understand why my Redux props are showing empty on the React component. This is the screenshot of the Redux state:

enter image description here

This is the component that visualizes. It is a child of a parent component that fetches the data and runs a for loop through the array of elements.

import React, { Component } from 'react'
import { connect } from "react-redux";
import PropTypes from "prop-types";

 class ResourceCard extends Component {
  render() {
    const { resource } = this.props;

    return (
      <div className="resource-card">
        <div className="resource-card-header">
        <span className="res-span-header">{resource.header}</span>
        </div>
        <div className="resource-card-image">
        <img src={resource.image} alt={resource.header} width='300px' />
        </div>
        <div className="resource-card-desc">
        <span className="res-span-desc">{resource.description}</span>
        </div>
        <div className="resource-card-link">
        <span className="res-span">{resource.website}</span>
        </div>
      </div>
    )
  }
}


ResourceCard.propTypes = {
  resource: PropTypes.object.isRequired,
  auth: PropTypes.object.isRequired
};

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

export default connect(
  mapStateToProps,
  {}
)(ResourceCard);

While the css is being rendered properly, and there are no errors in the console, the content itself is not there:

enter image description here

What could possibly be wrong here?

EDITED: Adding actions code:

// Get Resources
export const getResources = () => dispatch => {
  axios
    .get("/api/resources")
    .then(res =>
      dispatch({
        type: GET_RESOURCES,
        payload: res.data
      })
    )
    .catch(err =>
      dispatch({
        type: GET_RESOURCES,
        payload: null
      })
    );
};

And the reducer:

import {
  ADD_RESOURCE,
  GET_RESOURCES,
  DELETE_RESOURCE,
  GET_RESOURCE
} from "../actions/types";

const initialState = {
  resources: [],
  resource: {}
};

export default function(state = initialState, action) {
  switch (action.type) {
    case GET_RESOURCES:
      return {
        ...state,
        resources: action.payload
      };
    case GET_RESOURCE:
      return {
        ...state,
        resource: action.payload
      };
    case DELETE_RESOURCE:
      return {
        ...state,
        resources: state.resources.filter(resource => resource._id !== action.payload)
      };
    case ADD_RESOURCE:
      return {
        ...state,
        resources: [action.payload, ...state.resources]
      };
    default:
      return state;
  }
}

Parent component that runs a for-loop:

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import ResourceCard from './ResourceCard';

class ResourceFeed extends Component {
  render() {
    const { resources } = this.props;
    return resources.map(resource => <ResourceCard key={resource._id} resource={resource} />)
  }
}

ResourceFeed.propTypes = {
  resources: PropTypes.array.isRequired
}

export default ResourceFeed;

And the component above contains this parent:

<ResourceFeed resources={resources}/> 
2
  • 1
    please show actions code/reducer code Commented Jul 16, 2018 at 6:39
  • Provided in the edit. Commented Jul 16, 2018 at 6:42

1 Answer 1

2

Issue is here:

const mapStateToProps = state => ({
  auth: state.auth,
  resource: state.resource      // <========== in this line
});

In ResourceCard component you are passing the resource props from two places, one from parent component and one from redux store, that's why.

Your component is expecting the resource value from parent, so remove this line:

resource: state.resource

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. I am running a for-loop with mapping in the parent component of this one, passing down to this. Do I need to run another one inside of this one as well?
in that case you don't need to run another loop, can you show how you are running the loop in parent component?
Provided in the edit. And, btw, resource[0].header gives me 'undefined' TypeError.
check the updated answer, let me know if it fails to solve the issue :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.