reactjs - 无法使用 react 和 redux 处理打字稿
问题描述
我是打字稿的新手,真的很挣扎。我不明白从哪里开始,从哪里结束。是的,互联网上有很多资源,但我无法获得这些信息并在我的项目中使用。希望在这里遇到一些帮助。我也做了一些类型检查,如果你发现我可以做得更好的东西,请告诉我改进它。所以现在我正在与 reduxmapStateToProps
和mapDispatchToProps
. 我尝试了很多变体,但每次我都会遇到某种错误。我将发布代表连接到 state 的仪表板组件的代码。
import * as React from 'react';
import { connect } from 'react-redux';
import SearchIcon from '../SvgIcons';
import MapComponent from '../Map';
import { getEventInfo, getUserInfo } from '../../actions';
interface StateProps {
userReducer: {
accessToken: string
},
eventReducer: {
events: object[]
}
}
interface DispatchProps {
dispatchUserInfo: () => void;
dispatchEventInfo: (accessToken: string, query: string) => void;
}
interface OwnProps {
onSubmit: (e: React.FormEvent<HTMLFormElement>) => void,
accessToken: string,
events: object[]
}
type Props = StateProps & DispatchProps & OwnProps;
class DashboardPage extends React.Component<Props, {}> {
componentDidMount() {
const { dispatchUserInfo } = this.props;
dispatchUserInfo();
}
handleEventSearch = e => {
e.preventDefault();
const { dispatchEventInfo, accessToken } = this.props;
const query: string = e.target.children[0].value;
dispatchEventInfo(accessToken, query);
}
render() {
const { events } = this.props;
return (
<div className="dashboard-container">
<div className="search-event">
<form className="search-event__form" onSubmit={this.handleEventSearch}>
<input
autoComplete="off"
type="text"
name="search-event"
placeholder="Search an event"
className="search-event__input"
aria-label="Enter search text"
/>
<button type="submit" className="search-event__button">
<SearchIcon />
Search
</button>
</form>
<p className="sign-out">
<a href="/api/logout" className="btn btn--sign-out sign-out__button">Sign out</a>
</p>
</div>
<div className="google-map">
<MapComponent events={events} />
</div>
</div>
);
}
}
const mapStateToProps = (state: StateProps) => {
const accessToken = state.userReducer.accessToken || '';
const events = state.eventReducer || [];
return {
accessToken,
events
};
};
const mapDispatchToProps = (dispatch: DispatchProps) => ({
dispatchEventInfo(query: string, token: string) {
dispatch(getEventInfo(query, token));
},
dispatchUserInfo() {
dispatch(getUserInfo());
}
});
export default connect(mapStateToProps, mapDispatchToProps)(DashboardPage);
这些是打字稿错误
1) Refers to
handleEventSearch`方法
[ts] 参数 'e' 隐含地具有 'any' 类型。
2) 指mapDispatchToProps
[ts] 无法调用类型缺少调用签名的表达式。类型“DispatchProps”没有兼容的调用签名。
3) 指mapDispatchToProps
在connect HOC
类型参数 '(dispatch: DispatchProps) => { dispatchEventInfo(query: string, token: string): void; 调度用户信息():无效;}' 不能分配给“MapDispatchToPropsParam<{ dispatchEventInfo(query: string, token: string): void; 调度用户信息():无效;},{}>'。
类型 '(dispatch: DispatchProps) => { dispatchEventInfo(query: string, token: string): void; 调度用户信息():无效;}' 不能分配给类型 'MapDispatchToPropsFunction<{ dispatchEventInfo(query: string, token: string): void; 调度用户信息():无效;},{}>'。
参数“dispatch”和“dispatch”的类型不兼容。类型“Dispatch>”不可分配给类型“DispatchProps”。“Dispatch>”类型中缺少属性“dispatchUserInfo”。
另外,如果您可以为我提供非常好的资源来学习使用 typescript 的 react 和 redux,那么我可以轻松编写我的代码。
解决方案
您需要明确提供事件类型,
any
或者React.SyntheticEvent<...something>
调度参数应该是
Dispatch
来自 Redux 的类型
还有一点提示,您可以定义您的道具,例如:
ReturnType<typeof mapStateToProps> & ReturnType<typeof mapDispatchToProps> & OwnProps
并删除不必要的接口
推荐阅读
- trace32 - Trace32:当调试器处于运行模式时,Var.watch 窗口不显示变量值更新
- rpa - 循环转到 csv 中的特定单元格
- postgresql - 不是 100% 确定这是否安全
- deep-learning - 深度学习中的“小批量”是什么意思?
- microsoft-graph-api - 图“ErrorApiQuarantined”
- jquery - 如何使用 jquery 实时收听 div 并在鼠标滚轮上添加或删除类?
- c# - 如何在我的 xamarin android 应用程序中使用 SharpNLP
- jquery - jquery 无法使用 gridview 数据填充文本框
- c++ - 如何在 blynk (arduino) 中合并 2 个文件
- django - 当将namesilo域名指向AWS ec2实例而不使用路由53时,域名停留在nginx欢迎页面上