P
P
Pogran2016-08-25 15:28:40
React
Pogran, 2016-08-25 15:28:40

How to get list of entities after page load?

After page loading I want to deduce the list of objects from base.
component

import React from 'react';
import { connect } from 'react-redux';
import { fetchEntities } from '../../actions/entityActions';

class EntityList extends React.Component {
  componentWillMount() {
    this.props.fetchEntities();
  }

  render() {
    return (
      <div className="col-md-8">
        List Entities
      </div>
    );
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    fetchEntities: () => {
      dispatch(fetchEntities());
    }
  }
};

EntityList.propTypes = {
  fetchEntities: React.PropTypes.func.isRequired
};

export default connect(null, mapDispatchToProps)(EntityList);

action
export function fetchEntities() {
  
  const result = axios.get('/api/entities');
  
  return {
    type: FETCH_ENTITIES,
    entities: result
  }
}

Well, the reducer itself does not know how to do it correctly. now he looks like this
import {FETCH_ENTITIES} from '../actions/types';

const initialState = [];

export default (state = initialState, action = {}) => {
  switch (action.type) {
    case FETCH_ENTITIES:
      console.log(action.entities);
      return state;

    default:
      return state;
  }
};

it's just that if you write action.entities.then(res => console.log(res.data)) - then I get the data output, but I can't put it in the store

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question