マイコード:Reduxの/が反応:なぜ `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>
);
}
上記コードのコンソール出力(2つだけ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(一部削除されたUserオブジェクトの詳細):
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が1行でのみ呼び出されるのを見ると 'this.props.users.map'が参照されます。つまり' this.props.users'は未定義であるか、配列として正しく初期化されていません。 –
@DorWeid私の更新。ありがとうございました – BAE