Reactjs 使用typescript响应Redux
我正在用redux学习react和typescript。我有这样一个子组件:Reactjs 使用typescript响应Redux,reactjs,typescript,redux,Reactjs,Typescript,Redux,我正在用redux学习react和typescript。我有这样一个子组件: import ... interface MainTestProps { loadData: () => () => void, students: StudentsModel[], state: string, errorMessage?:string } interface MainTestState { } class MainTest extends React.Comp
import ...
interface MainTestProps {
loadData: () => () => void,
students: StudentsModel[],
state: string,
errorMessage?:string
}
interface MainTestState {
}
class MainTest extends React.Component<MainTestProps,MainTestState>{
constructor(props: MainTestProps, state: MainTestState){
super(props,state);
}
.... rest of component
const mapStateToProps = (state:AppState, ownProps: MainTestProps)=> {
return {
students: [],
state: 'INIT',
errorMessage:''
}
}
const mapDispatchToProps = (dispatch: any) => {
return {
loadData: () => dispatch(actionFetchStudents())
}
}
export default connect(mapStateToProps,mapDispatchToProps) (MainTest);
为什么如果调用此组件,我会收到错误类型“{}”缺少类型“Readonly&MainTestProps>”中的以下属性:loadData、students、state ts2739。我的意思是我不想通过属性,我正在使用redux!!!typescript redux库中是否存在错误,或者为什么我必须传递数据,或者我对mapStateToProps的声明是否错误,以及我必须禁用参数ownProps
谢谢大家
Arnold您能否演示如何调用此MainTest组件?由于您只是通过Redux添加loadData,其他参数student和state仍应作为prop传递给组件。errorMessage属性是可选的,因此transpiler不会对此抱怨 transpiler抱怨loadData属性的原因很可能是因为您在MainTestProps中提供的类型与loadData函数的类型不对应。我只能猜测,因为我没有actionFetchStudents的实际实现,但loadData的类型应该是loadData:=>void;,这似乎是合理的;,而不是loadData:=>=>void 看看这是否有效,否则请提供actionFetchStudents的实现以及调用MainTest的函数 您可以使用类似这样的东西,这与react 16.8.4兼容
interface State {}
interface OwnProps {}
interface DispatchProps {
login: () => void;
}
interface StateProps {
isFetching: boolean;
accessToken: string;
}
type Props = StateProps & OwnProps & DispatchProps;
const ExampleTH = (props: Props): ReactElement => {