Unable to display data in component with react and redux

末鹿安然 提交于 2019-12-19 10:20:58

问题


I'm trying to display some dummy data on my component, but I'm not seeing anything appearing on it. When I console.log the expected result I get [object Object].

I think I'm missing something with my actions,actionCreators and reducers.

#actions/types.js

export const FETCH_USERS = 'fetch_users';



#actions/index.js   

import {
  FETCH_USERS
} from './types';

const user = [
  { name: 'D/S' },
  { name: 'Bob' },
  { name: 'Juan' }
]

export function fetchUsers() {
  return { type: FETCH_USERS, payload: user }
}



#reducers/users_reducer.js

import {
  FETCH_USERS
} from '../actions/types';

export default function (state = [], action) {
  switch(action.type) {
    case FETCH_USERS:
      return [ ...state, ...action.payload ];
  }
  return state;
}   


#reducers/index.js

import { combineReducers } from 'redux';
import UsersReducer from './users_reducer';


const rootReducer = combineReducers({
  users: UsersReducer
});

export default rootReducer;



# components/UserList.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import styles from './App.css';
import * as actions from '../actions';


class UserList extends Component {

  componentWillMount() {
    const fetchedUsers = this.props.fetchUsers() ? this.props.fetchUsers() : 'No users at this time';
    console.log(`This is the list of users: ${fetchedUsers}`); // <= here I just get [object Object]
  }

  renderUser(user) {
    <div className={ styles.card }> 
      <h4>{ user.name }</h4>
      <p>Lego Inc</p>
      <a>Email</a>
    </div>
  }

  render() {
    const userList = this.props.users.map(this.renderUser);
    console.log(`${userList}`);
    return (
     <div>
       { userList }
     </div> 
    )
  } 
}

function mapStateToProps(state) {
  return { users: state.users }
}
export default connect(mapStateToProps, actions)(UserList);



# components/App.js

import React from 'react';
import styles from './App.css';
import UserList from './UserList';

const App = () => (
  <div className={''}>
    <h2>React Redux middleware</h2>
    <div>
        <UserList />
    </div>
  </div>
);

export default App;

回答1:


You are not returning the JSX content inside your map function.

 renderUser(user) {
    return (           // Added a return statement here
    <div className={ styles.card }> 
      <h4>{ user.name }</h4>
      <p>Lego Inc</p>
      <a>Email</a>
    </div>
    )
  }

Also you need to use console.log(${userList}) is not required, console.log(userList) will work, however that not relevant to the problem. Just wanted to add to the answer




回答2:


I think you are missing the ReactDOM.render in order to actually mount the component to the DOM.

# components/App.js

import React from 'react';
import ReactDOM from 'react-dom';
import styles from './App.css';
import UserList from './UserList';

const App = () => (
  <div className={''}>
    <h2>React Redux middleware</h2>
    <div>
        <UserList />
    </div>
  </div>
);

ReactDOM.render(App, document.getElementById('react-app'));


来源:https://stackoverflow.com/questions/43897945/unable-to-display-data-in-component-with-react-and-redux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!