0
我使用反應和終極版和我有一個對象,它是對終極版商店,上面的截圖是在終極版被映射到一個組件內的道具的數據狀態:
class Users extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
this.props.dispatch(fetchUsers());
}
render() {
console.log(this.props.users);
return (
<div className="users">
{React.cloneElement(this.props.children, {
this.props.users
})}
</div>
);
}
}
const mapStateToProps = (state) => ({
users: state.users[0]
});
export default connect(mapStateToProps)(Users);
// reducer
import { createReducer } from '../utils/helpers';
users: createReducer(initialData, {
[FETCH_USERS](state, action) {
return [action.payload, ...state];
}
}),
//saga
function* watchFetchUsers() {
while(true) {
yield take(FETCH_USERS);
try {
const users = yield call(api.fetchUserData);
yield put({ type:FETCH_USERS, payload: users });
} catch (err) {
yield put({ type:SIGNOUT, status: err });
}
}
}
的問題是,我可以登錄這個數據與console.log(this.props.users)
但內的console.log(this.props.users.data)
或console.log(this.props.users.data[0])
它帶回來錯誤「數據」未定義我無法訪問數據。任何人都可以告訴我問題是什麼?
這個問題很混亂......用戶是一個數組還是一個對象?這個用戶是異步獲取的嗎? – Canastro
道具用戶是一個對象。是的,我有一個操作派發,用於獲取componentWillMount()函數中的用戶。 –
你可以發佈你的減速器代碼,所以我們可以更好地瞭解狀態? –