【问题标题】:Unable to display data in component with react and redux无法使用 react 和 redux 在组件中显示数据
【发布时间】:2017-05-10 16:20:07
【问题描述】:

我正在尝试在我的组件上显示一些虚拟数据,但我没有看到任何东西出现在它上面。当我console.log 得到预期结果时,我得到[object Object]

我想我的actionsactionCreatorsreducers 缺少一些东西。

#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;

【问题讨论】:

    标签: javascript reactjs redux


    【解决方案1】:

    您没有返回map function 中的JSX 内容。

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

    您还需要使用 console.log(${userList}) 不是必需的,console.log(userList) 可以工作,但这与问题无关。只是想补充一下答案

    【讨论】:

    • 该死的天哪,谢谢它解决了我的问题。我在敲我的头。
    • 这只是我们所有人有时都会犯的那个小错误,没问题,很高兴能提供帮助:)
    【解决方案2】:

    我认为您缺少 ReactDOM.render 以便将组件实际安装到 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'));
    

    【讨论】:

    • 不,我有另一个文件 index.js 我在其中加载了我的路由器。我只是没有在代码中添加它。感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2019-04-28
    • 2016-05-28
    • 1970-01-01
    • 2020-11-15
    • 2021-04-20
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多