我的代码:终极版/阵营:为什么`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