我正在尝试按照Redux Getting Started和Proper Typing of react-redux Connected Components中的文档使用Typescript编写React Redux组件,并且在我的connect
函数上遇到类型错误。
我的应用使用Redux维护问题表。
state.tsx
import {AnyAction, combineReducers} from "redux";
const ADD_QUESTION = "ADD_QUESTION";
export interface Question {
id: number,
text: string
}
export interface State {
questions: QuestionsTable
}
interface QuestionsTable {
nextId: number,
questions: Question[]
}
const questions = (state: State = {questions: {nextId: 0, questions: []}}, action: AnyAction): State => {
const questions = state.questions;
switch (action.type) {
case ADD_QUESTION:
return {
...state,
questions: {
...questions,
nextId: questions.nextId + 1,
questions: questions.questions.concat([{id: questions.nextId, text: action.text}])
}
};
default:
return questions
}
};
export const reducers = combineReducers({
questions
});
Questions
组件显示它们。我使用connect
从中创建QuestionsContainer
。
questions.tsx
import React from "react"
import {Question, State} from "../state";
import {connect} from "react-redux";
export type QuestionsProps = {
questions: Question[]
}
const Questions: React.FC<QuestionsProps> = ({questions}) => {
return (
<div>
<ul>
{questions.map(question => (
<li>{question.text}</li>
))}
</ul>
</div>
)
};
const mapStateToProps = (state: QuestionsTable): QuestionsProps => {
return {questions: state.questions.questions};
};
export const QuestionsContainer = connect<QuestionsProps>(mapStateToProps)(Questions);
我的顶级应用程序显示此容器组件。
App.tsx
import React from "react";
import "./App.css";
import {reducers} from "./state";
import {createStore} from "redux"
import {Provider} from "react-redux"
import {QuestionsContainer} from "./components/questions";
const store = createStore(reducers);
const App: React.FC = () => {
return (
<Provider store={store}>
<div className="App">
<QuestionsContainer/>
</div>
</Provider>
);
};
export default App;
调用connect
时出现类型错误。
Error:(61, 59) TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type '(state: State) => QuestionsProps' is not assignable to parameter of type 'MapStateToPropsParam<QuestionsProps, {}, {}>'.
Type '(state: State) => QuestionsProps' is not assignable to type 'MapStateToPropsFactory<QuestionsProps, {}, {}>'.
Types of parameters 'state' and 'initialState' are incompatible.
Property 'questions' is missing in type '{}' but required in type 'State'.
如果我用@ts-ignore
抑制了此错误,并记录了传递到我的questions
组件的Questions
的值,那么我会看到。
{"nextId":0,"questions":[]}
即使nextId
取消引用mapStateToProps
,我也不知道为什么state.questions.questions
字段在那里。
正确的设置方法是什么?
答案 0 :(得分:1)
好的 我会通过电话尝试 抱歉,格式化
import {AnyAction, combineReducers} from "redux";
const ADD_QUESTION = "ADD_QUESTION";
export interface Question {
id: number,
text: string
}
interface State {
nextId: number,
items: Question[]
}
const initialState: State = {nextId: 0, items: []}
const questions = (state = initialState, action: AnyAction): State => {
switch (action.type) {
case ADD_QUESTION:
return {
...state,
items: action.items,
nextId: action.nextId
};
default:
return state
}
};
export const reducers = combineReducers({
questions
});
这就是我看你的减速器
然后在mapStateToProps中的组件中 问题:state.questions.items