我的代碼:終極版/陣營:爲什麼`this.props`不確定
render() {
console.log('render, state' + Array.isArray(this.props.users) + ',' + JSON.stringify(this.props.users));
return (
<section>
<div>testing</div>
<div>{JSON.stringify(this.props.users)}</div>
<ul>
{this.props.users.map(user => <li>{user.email}</li>)}
</ul>
</section>
);
}
兩個<div>testing</div>
和<div>{JSON.stringify(this.props.users)}</div>
做工精細(除去<ul>...</ul>
後)。但<ul>...</ul>
沒有工作。錯誤是:
Uncaught TypeError: Cannot read property 'map' of undefined
歡迎任何評論。由於
UPDATE
下列代碼做工精細,this.props.users
是一個數組,並且所有的控制檯日誌看起來OK。 我只需要知道爲什麼<ul>{this.props.users.map(user => <li>{user.email}</li>)}</ul>
不起作用。爲什麼this.props.users
在<ul>...</ul>
未定義。
render() {
console.log('render, state' + Array.isArray(this.props.users) + ',' + JSON.stringify(this.props.users));
return (
<section>
<div>testing</div>
<div>{JSON.stringify(this.props.users)}</div>
</section>
);
}
上述碼的控制檯輸出(僅兩個s):
render, statetrue,[{"_id":"5831e6df511e","updatedAt":"removed","createdAt":"removed","email":"removed","password":"xxxx","__v":0,"role":"xxx","profile":{"firstName":"test","lastName":"tester"}},{...}, {...}]
UPDATE
我的代碼:
import { connect } from 'react-redux';
import { fetchAllUsers } from '../../actions/index';
class HomePage extends Component {
componentWillMount() {
console.log('componentWillMount()');
this.props.fetchAllUsers();
}
render() {
console.log('render(), ' + Array.isArray(this.props.users) + ',' + JSON.stringify(this.props.users));
return (
<section>
<div>{JSON.stringify(this.props.users)}</div>
</section>
);
}
}
function mapStateToProps(state) {
console.log('state:' + JSON.stringify(state));// working fine, and the console.log is OK. not added to this post
return {
users: state.admin.users
}
}
export default connect(mapStateToProps, {fetchAllUsers})(HomePage);
的console.log(一些用戶對象中的詳細信息已刪除。):
render(), true,[{"_id":"5831","profile":{"firstName":"test","lastName":"tester"}},{"_id":"5831e874cedf511f", "profile":{"firstName":"cccc","lastName":"cc"}},{"_id":"5831120","profile":{"firstName":"cccccccc","lastName":"ccc"}}]
而且,在網頁上顯示了this.props.users
的字符串。
我的問題是爲什麼<ul>{this.props.users.map(user => <li>{user.email}</li>)}</ul>
不工作,但<div>{JSON.stringify(this.props.users)}</div>
工作正常。
我想我已經清楚地描述了我的問題。如果需要更多信息,請告訴我。
更多細節
我admin_reducer.js
import { FETCH_ALL_USERS } from '../actions/types';
const INITIAL_STATE = { users:[] };
export default function (state = INITIAL_STATE, action) {
console.log('users is action.payload:'+JSON.stringify({ users:action.payload }));
return Object.assign({}, state, {users:action.payload});
//return { ...state, users:action.payload };
}
我index.js
import adminReducer from './admin_reducer';
const rootReducer = combineReducers({
...
admin: adminReducer
});
出口默認rootReducer;
my action.js
export function fetchAllUsers() {
return function(dispatch) {
axios.get(`${API_URL}/user/all`)
.then(response => {
dispatch({
type: FETCH_ALL_USERS,
payload: response.data
});
})
.catch(response => dispatch(errorHandler(response.data.error)))
}
}
UPDATE
爲什麼console.log(this.props.users)
控制檯日誌是[object Object],[object Object],[object Object]
?
'thi s.props'根本沒有定義,你的問題是錯誤的。 –
它不能讀取未定義的屬性'map'。由於只能在一行中調用map,它引用'this.props.users.map',這意味着'this.props.users'要麼是未定義的,要麼是未被初始化爲數組 –
@DorWeid請參見我的更新。謝謝 – BAE