Follow the official doc, I make a login typescript like thunk function.
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
})
)
}
I want add this thunk function to my app component using react-redux connect hoc function.
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)
It report a error show that the thunkLogin declare at Props not assignable to mapDispatchToProps (place 1 -> place 2).