1

これらの技術に新しく、私のシングルページアプリケーションに関する質問があります。私は、このテンプレートからreactredux例を開始しました:Redux + React + Typescript - ナビゲーションバーの選択されたインデックス状態を保存する適切な方法

https://blogs.msdn.microsoft.com/webdev/2017/02/14/building-single-page-applications-on-asp-net-core-with-javascriptservices/

私のSPAのルートコンポーネントは、レイアウトです。 NavMenuはレイアウトの下にあり、RightNavMenuオブジェクトの直下にあります。 RightNavMenuがNavMenuの状態に応じて変化、Iセットアップので、レイアウトレベルでNavMenuストアは、次のように:

index.ts:ここ

import * as NavMenu from './NavMenu'; 

// The top-level state object 
export interface ApplicationState {  
navMenu: NavMenu.NavMenuState //-stores information on which index is currently selected 
} 

// Whenever an action is dispatched, Redux will update each top-level application state property using 
// the reducer with the matching name. It's important that the names match exactly, and that the reducer 
// acts on the corresponding ApplicationState property type. 
export const reducers = { 
navMenu: NavMenu.reducer 
}; 

// This type can be used as a hint on action creators so that its 'dispatch' and 'getState' params are 
// correctly typed to match your store. 
export interface AppThunkAction<TAction> { 
(dispatch: (action: TAction) => void, getState:() => ApplicationState): void; 
} 

私NavMenuストア(NavMenu.ts)は次のとおりです。 import {Action、Reducer}から 'redux'をインポートします。ここで

export interface NavMenuState { 
selectedIndex: number;  
} 

interface SelectIndexAction { type: 'NAV_SELECT_INDEX' } 
interface DoNothingAction { type: 'NAV_DO_NOTHING' } 

type KnownAction = SelectIndexAction | DoNothingAction; 

export const actionCreators = { 
select: (selectedIndex:number) => <SelectIndexAction>{ type: 'NAV_SELECT_INDEX' }, 
donothing:() => <DoNothingAction>{ type: 'NAV_DO_NOTHING' } 
}; 

export const reducer: Reducer<NavMenuState> = (state: NavMenuState, action: KnownAction) => { 
switch (action.type) { 
    case 'NAV_SELECT_INDEX': 
     alert('hasdf'); 
     return { selectedIndex: state.selectedIndex };   
    case 'NAV_DO_NOTHING': 
     return { selectedIndex: state.selectedIndex }; 
    default: 
     // The following line guarantees that every action in the  KnownAction union has been covered by a case above 
     const exhaustiveCheck: never = action; 
} 

// For unrecognized actions (or in cases where actions have no effect), must return the existing state 
// (or default initial state if none was supplied) 
return state || { selectedIndex: 0 }; 
}; 

は私のNavMenu.tsxコンポーネントです:

import * as React from 'react'; 
import { Link } from 'react-router'; 
import { connect } from 'react-redux'; 
import { ApplicationState } from '../store'; 
import { List, ListItem, makeSelectable } from 'material-ui/List'; 
import * as NavMenuStore from '../store/NavMenu'; 

let SelectableList = makeSelectable(List); 
type NavMenuProps = NavMenuStore.NavMenuState 
& typeof NavMenuStore.actionCreators 
& { params: { startIndex: string } };  // ... plus incoming routing  parameters 


export class NavMenu extends React.Component<NavMenuProps, null> { 
componentWillMount() { 

    // This method runs when the component is first added to the page 
    let selectedIndex = parseInt(this.props.params.startIndex) || 0; //parse the int or set to 0 if it fails 
    if (selectedIndex == 0) //if the parse fails select the first index by defaualt 
     selectedIndex = 1; 
    this.props.select(selectedIndex); 
} 

public render() { 
    return ( 
     <SelectableList defaultValue={1}> 
      <p>Current index: <strong>{this.props.selectedIndex}</strong>  </p> 
      <ListItem onClick={() => { this.props.select(1) }} value={1} primaryText="TestIndex1" />    
      <ListItem onClick={() => { this.props.select(2) }} value={2} primaryText="TestIndex2" />    
     </SelectableList> 
    ); 
} 

} 

export default connect(
(state: ApplicationState) => state.navMenu, // Selects which state properties are merged into the component's props 
NavMenuStore.actionCreators     // Selects which action creators are merged into the component's props 
)(NavMenu); 

は最後に、これは私が私のレイアウト・コンポーネントでそれを埋め込むい方法です:

<NavMenu /> 

以上不足しているというエラーがスローされます私はこれに変更しようとしましたが、それでも動作しません。

<NavMenu donothing={null} params={null} selectedIndex={0} select={ null } /> 

私が間違っていることを教えてもらえますか?私は現在、選択したインデックスをリストの先頭に出力しようとしています。ユーザーがリストの最初または2番目の項目をクリックすると、選択した索引テキストがそれに応じて更新されると考えられます。

質問:

  1. 私はレイアウトと右のナビゲーションバーで正しくナビゲーションバー構造を実装していますか?

  2. 私はどのように動作させることができますか?

ありがとうございます。

答えて

1

このポストはちょっと古いですが、他の誰かがこの問題に遭遇した場合に備えて私の経験を共有すると思いました。私は2日間正確に同じ問題を戦って、ついにそれを働かせました。

私にとっては、2つのことに似ているようでした。 、別のコンポーネントにしたときに、「インポートNavMenu」、私が代わりにexportキーワードであることを推測している

class NavMenu extends React.Component<NavMenuProps, null> { 

export class NavMenu extends React.Component<NavMenuProps, null> { 

をし、「エクスポート」キーワードを脱いだ:まず、私は変更しましたReduxのConnectオブジェクトにラップされていない実際のNavMenuクラスを取得しています。

また、マークアップ内にNavMenuがあるLayout.tsxファイルでは、読み込み文が間違っていたようです。私はそれを次のように変更しました:

import NavMenu from './NavMenu'; 

これはすべて私のために今働きます。 申し訳ありませんが私の答えは少し混乱または不正確です。私はReactに非常に新しいですし、JavaScriptの専門家の多くもそうではありませんので、私に炎を吹かないでください。

関連する問題