Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React、Redux和Typescript-如何使其工作_Reactjs_Typescript_Redux - Fatal编程技术网

Reactjs React、Redux和Typescript-如何使其工作

Reactjs React、Redux和Typescript-如何使其工作,reactjs,typescript,redux,Reactjs,Typescript,Redux,我是新的反应和重复,以前我只使用角度。我的第一个学习问题是在尝试使用Redux时出现的。我已经在index.tsx文件中定义了我的简单状态、操作、缩减器和存储: export interface AppState { count: number; } const INCREMENT = 'INCREMENT'; export class IncrementAction implements Action { type = INCREMENT; } function opsRe

我是新的反应和重复,以前我只使用角度。我的第一个学习问题是在尝试使用Redux时出现的。我已经在
index.tsx
文件中定义了我的简单状态、操作、缩减器和存储:

export interface AppState {
    count: number;
}

const INCREMENT = 'INCREMENT';
export class IncrementAction implements Action {
    type = INCREMENT;
}

function opsReducer(state: AppState = {} as AppState, action: IncrementAction): AppState {
    console.log("ops reducer", action);
    switch (action.type) {
        case INCREMENT:
            return { ...state, count: state.count + 1 } as AppState;
        default:
            return state;
    }
}

const rootReducer = combineReducers({
    ops: opsReducer
});

const store = createStore(rootReducer);

ReactDOM.render(
    <Provider store={store}>
        <App appName="Test" />
    </Provider>,
    document.getElementById('root') as HTMLElement
);
导出接口AppState{
计数:数字;
}
常量增量=‘增量’;
导出类IncrementAction实现操作{
类型=增量;
}
函数opsReducer(状态:AppState={}作为AppState,操作:IncrementAction):AppState{
控制台日志(“ops减速器”,动作);
开关(动作类型){
案例增量:
返回{…state,count:state.count+1}作为AppState;
违约:
返回状态;
}
}
const rootReducer=combinereducer({
操作:操作还原器
});
const store=createStore(rootReducer);
ReactDOM.render(
,
document.getElementById('root')作为HTMLElement
);
并修改了应用程序组件,使其连接起来,看起来像这样

interface StateProps {
    appName: string;
}

interface DispatchProps {
    increment: () => void;
}

class App extends React.Component<StateProps & DispatchProps> {
    render() {
        return (
            <div className="App">
                <button onClick={this.props.increment}>CLICK ME {this.props.appName}</button>
            </div>
        );
    }
}

function mapDispatchToProps(dispatch: Dispatch<AppState>) {
    return {
        increment: () => dispatch(new IncrementAction())
    } as DispatchProps;
}

export default connect<StateProps, DispatchProps>(null, mapDispatchToProps)(App);
interface StateProps{
appName:string;
}
界面分派道具{
增量:()=>void;
}
类应用程序扩展了React.Component{
render(){
返回(
单击我{this.props.appName}
);
}
}
功能图DispatchToprops(调度:调度){
返回{
增量:()=>分派(新增量操作())
}作为派遣道具;
}
导出默认连接(空,mapDispatchToProps)(应用程序);
index.tsx文件中存在错误:

Type '{}' is not assignable to type 'Readonly<Pick<StateProps & DispatchProps, "appName">>'.
Property 'appName' is missing in type '{}'.
类型“{}”不能分配给类型“Readonly”。
类型“{}”中缺少属性“appName”。

如何修复它?如何让所有这些东西与TypeScript的硬输入一起工作?当我最终修复它时,如何组织源代码?哪些内容应该移动到单独的文件中?我喜欢基于特征的代码分离。如何使用React和Redux实现这一点?

我认为这里的关键问题是
函数opsReducer
。您所说的
state
的类型是
AppState
,初始值是空对象。相反,
{}
这样写:

function opsReducer(state: AppState = { count: 0 }, action: IncrementAction): AppState {
    console.log("ops reducer", action);
    switch (action.type) {
        case INCREMENT:
            return { ...state, count: state.count + 1 };
        default:
            return state;
    }
}

我认为这里的关键问题是
函数opsReducer
。您所说的
state
的类型是
AppState
,初始值是空对象。相反,
{}
这样写:

function opsReducer(state: AppState = { count: 0 }, action: IncrementAction): AppState {
    console.log("ops reducer", action);
    switch (action.type) {
        case INCREMENT:
            return { ...state, count: state.count + 1 };
        default:
            return state;
    }
}

@micnyk如果这个解决方案适合您,请将答案标记为正确。谢谢。@micnyk如果此解决方案对您有效,请将答案标记为正确。非常感谢。