【发布时间】:2023-03-31 01:18:01
【问题描述】:
直接在 React 组件中使用 async/await 然后将结果存储在 store 中是一种好习惯吗?例如:
class User extends Component {
render() {
return <div>{this.props.user.name}</div>
}
componentWillMount() {
this.getUser();
}
async getUser() {
try {
const user = await userAction.get();
this.props.storeUser(user);
} catch (err) {}
}
}
const state2props = (state) => ({
user: state.User.user
});
const dispatch2props = dispatch => ({
storeUser: (user) => dispatch(userReducer.store(user)),
});
export default connect(state2props, dispatch2props)(User);
它似乎比经典的 react/redux 模式更灵活。
【问题讨论】:
标签: reactjs redux async-await