前提・実現したいこと
React + Redux + TypeScript でサイドメニュー用のボタンを実装しようとしています。
Qiitaなどを参考に書いてみたのですが、いまいち問題になっている理由などわからないので質問させて頂きました。
発生している問題・エラーメッセージ
Argument of type 'StyledComponent<PropsWithChildren<OwnProps & DrawerState & DrawerActions & { className?: string | undefined; }>, Pick<PropsWithChildren<OwnProps & DrawerState & DrawerActions & { className?: string | undefined; }>, "drawn" | ... 2 more ... | "children">, object>' is not assignable to parameter of type 'ComponentType<Matching<DrawerState & { updateDrawer: () => void; }, OwnProps & DrawerState & DrawerActions & { className?: string | undefined; } & { children?: ReactNode; } & Pick<PropsWithChildren<OwnProps & DrawerState & DrawerActions & { ...; }>, "drawn" | ... 2 more ... | "children"> & { ...; }>>'. Type 'StyledComponent<PropsWithChildren<OwnProps & DrawerState & DrawerActions & { className?: string | undefined; }>, Pick<PropsWithChildren<OwnProps & DrawerState & DrawerActions & { className?: string | undefined; }>, "drawn" | ... 2 more ... | "children">, object>' is not assignable to type 'FunctionComponent<Matching<DrawerState & { updateDrawer: () => void; }, OwnProps & DrawerState & DrawerActions & { className?: string | undefined; } & { children?: ReactNode; } & Pick<PropsWithChildren<OwnProps & DrawerState & DrawerActions & { ...; }>, "drawn" | ... 2 more ... | "children"> & { ...; }>>'. Types of parameters 'props' and 'props' are incompatible. Type 'PropsWithChildren<Matching<DrawerState & { updateDrawer: () => void; }, OwnProps & DrawerState & DrawerActions & { className?: string | undefined; } & { children?: ReactNode; } & Pick<PropsWithChildren<OwnProps & DrawerState & DrawerActions & { ...; }>, "drawn" | ... 2 more ... | "children"> & { ...; }>>' is not assignable to type 'OwnProps & DrawerState & DrawerActions & { className?: string | undefined; } & { children?: ReactNode; } & Pick<PropsWithChildren<OwnProps & DrawerState & DrawerActions & { className?: string | undefined; }>, "drawn" | ... 2 more ... | "children"> & { ...; }'. Type 'PropsWithChildren<Matching<DrawerState & { updateDrawer: () => void; }, OwnProps & DrawerState & DrawerActions & { className?: string | undefined; } & { children?: ReactNode; } & Pick<PropsWithChildren<OwnProps & DrawerState & DrawerActions & { ...; }>, "drawn" | ... 2 more ... | "children"> & { ...; }>>' is not assignable to type 'DrawerActions'. Types of property 'updateDrawer' are incompatible. Type '() => void' is not assignable to type '() => Action<{}>'. Type 'void' is not assignable to type 'Action<{}>'.ts(2345)
該当のソースコード
DrawerActions
typescript
1import actionCreatorFactory from 'typescript-fsa'; 2 3const actionCreator = actionCreatorFactory(); 4 5export const DrawerActions = { 6 updateDrawer: actionCreator('ACTIONS_UPDATE_DRAWER') 7};
DrawerState.tsx
typescript
1import { reducerWithInitialState } from 'typescript-fsa-reducers'; 2import { DrawerActions } from '../actions/DrawerActions'; 3 4export interface DrawerState { 5 drawn: boolean; 6} 7 8const initialState: DrawerState = { 9 drawn: true 10}; 11 12export const DrawerReducer = reducerWithInitialState(initialState) 13 .case(DrawerActions.updateDrawer, (state=initialState) => { 14 return Object.assign({}, state, { drawn: !state.drawn }) 15});
DrawerStore.tsx
typescript
1import { createStore, combineReducers } from 'redux'; 2import { DrawerReducer, DrawerState } from '../states/DrawerState'; 3 4export type AppState = { 5 drawn: DrawerState 6}; 7 8const store = createStore( 9 combineReducers<AppState>({ 10 drawn: DrawerReducer 11 }) 12); 13 14// const store = createStore(DrawerReducer) 15 16export default store;
container.tsx
typescript
1import { Action } from 'typescript-fsa'; 2import { Dispatch } from 'redux'; 3import { connect } from 'react-redux'; 4import { AppState } from '../../stores/DrawerStore'; 5import { DrawerActions } from '../../actions/DrawerActions'; 6import { StyledSideMenu } from './sidemenu'; 7 8export interface DrawerActions { 9 updateDrawer: () => Action<{}>; 10} 11 12 13const mapDispatchToProps = (dispatch: Dispatch) => { 14 return { 15 updateDrawer: () => { dispatch(DrawerActions.updateDrawer()) } 16 } 17} 18 19const mapStateToProps = (appState: AppState) => { 20 return Object.assign({}, appState.drawn); 21} 22 23export default connect(mapStateToProps, mapDispatchToProps)(StyledSideMenu);
sidemenu.tsx
typescript
1import React from 'react'; 2import { Nav } from './nav'; 3import { useSpring, animated } from "react-spring"; 4import { DrawerState } from '../../states/DrawerState'; 5import { DrawerActions } from './container' 6 7interface OwnProps {} 8 9type DrawerProps = OwnProps & DrawerState & DrawerActions; 10 11const SideMenu: React.FCX<DrawerProps> = (props: DrawerProps, { className }) => { 12 13 const settings = useSpring({ 14 from: { opacity: 0 }, 15 to: { 16 opacity: props.drawn ? 1 : 0, 17 transform: props.drawn ? 'translate3d(0px, 0, 0)' : 'translate3d(200px, 0, 0)' 18 }, 19 config: { mass: 0.7, friction: 20, tension: 100 } 20 }); 21 return ( 22 <> 23 <button onClick={() => props.updateDrawer() }> 24 toggle 25 </button> 26 <animated.nav className={className} style={{ ...settings }}> 27 <Nav /> 28 </animated.nav> 29 </> 30 ); 31};
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。