質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Redux

Reduxは、JavaScriptアプリケーションの状態を管理するためのオープンソースライブラリです。ReactやAngularで一般的にユーザーインターフェイスの構築に利用されます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

1回答

439閲覧

React + Redux + TypeScriptでのボタン実装

totoga

総合スコア4

Redux

Reduxは、JavaScriptアプリケーションの状態を管理するためのオープンソースライブラリです。ReactやAngularで一般的にユーザーインターフェイスの構築に利用されます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2019/12/30 13:49

前提・実現したいこと

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};

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

型を割り当てられない、パラメータが非互換などと表示されてますのでそれらが理由かと思います。
一つ一つ型を修正していきましょう。

投稿2019/12/31 03:45

tutti56

総合スコア86

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問