我已經創建了一個組件,並從userReducer獲取數據並將其綁定到道具,但在呈現方法中,如果我控制數據(即用戶數組),我可以在控制檯中看到數據但是當我的數據傳遞給一個函數,並嘗試循環陣列上,我得到以下錯誤無法循環一個數組
遺漏的類型錯誤:無法讀取的不確定
我不理解,我讓財產「地圖」任何錯誤。
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux'
import * as userActions from '../../actions/userActions'
class HomePage extends Component {
constructor(props){
super(props);
this.state = {
isLoading :true
}
}
componentDidMount(){
const {dispatch} = this.props
dispatch(userActions.loadUsers());
}
renderData(data){
debugger
data.map(item=>{
return (<div>
<h1>{item.id}</h1>
</div>)
})
}
render() {
// if(!this.props.usersReducer) return <p>Loading ....</p>
return (
<div>
{console.log(this.props.userReducer.data)}
{this.renderData(this.props.userReducer.data)} // if i comment this line i can see the users array in console but if i uncomment this line i am getting undefined in console for console.log(this.props.userReducer.data) too
</div>
);
}
}
HomePage.propTypes = {};
HomePage.defaultProps = {};
const mapStateToProps = (state, props) =>
({
userReducer : state.userReducer
})
export default connect(mapStateToProps)(HomePage);
'this.props.userReducer.data'的值是什麼? – imcvampire
你是什麼原因返回一個div? 我認爲它的翻譯方式存在一些問題 –
嘗試刪除'debugger'並將缺失的返回添加到'renderData' – dostu