【发布时间】:2023-10-01 20:17:01
【问题描述】:
关注official doc,我做了一个类似thunk函数的登录打字稿。
function loginApi(user: UserState) {
return fetch(
`${baseUrl}/login?user=${user.userName}&pwd=${user.pwd}`
)
.then(res => res.json())
}
export const thunkLogin = (
user: UserState
): ThunkAction<void, AppState, null, Action<string>> => async dispatch => {
const asyncResp = await loginApi(user)
dispatch(
updateUser({
loggedIn: asyncResp.isloggedIn,
userName: user.userName,
userPwd: user.userPwd
})
)
}
我想使用 react-redux connect hoc 函数将此 thunk 函数添加到我的应用程序组件中。
import { thunkLogin } from './thunkLoginPath'
interface Props {
// place 1
thunkLogin: typeof thunkLogin
}
interface State {
userName: string
userPwd: string
}
class AppComponent extends React.Component<Props, State> {
handleSubmit = () => {
this.props.thunkLogin({
userName: this.state.userName,
userPwd: this.state.userPwd
})
}
render(){
return(
<TouchableOpacity
style={style.loginBotton}
onPress={this.handleSubmit}
>
<Text style={style.loginBottomText}>LOGIN</Text>
</TouchableOpacity>
)
}
}
export default connect(
(state: AppState) => ({
user: state.user
}),
// place 2
{ thunkLogin }
)(AppComponent)
报错显示在 Props 中声明的 thunkLogin 不能分配给 mapDispatchToProps (place 1 -> place 2)。
【问题讨论】:
标签: typescript react-native redux react-redux