首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Redux+React+Typescript -保存导航栏的选定索引状态的正确方法是什么?

Redux+React+Typescript -保存导航栏的选定索引状态的正确方法是什么?
EN

Stack Overflow用户
提问于 2017-03-12 19:16:12
回答 1查看 822关注 0票数 1

新的这些技术,并有一个关于我的单页应用程序的问题。我从这个模板中的reactredux示例开始:

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

我的SPA的根组件是布局。NavMenu直接位于布局和RightNavMenu对象之下。RightNavMenu根据NavMenu的状态变化,因此我在布局级别上设置了NavMenu存储,如下所示:

index.ts:

代码语言:javascript
运行
复制
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):从‘redux’导入{ Action,Reducer };

代码语言:javascript
运行
复制
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组件:

代码语言:javascript
运行
复制
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);

最后,我是如何将其嵌入到布局组件中的:

代码语言:javascript
运行
复制
<NavMenu /> 

以上抛出一个关于缺少属性的错误,因此我尝试更改为这个,但它仍然不能工作。

代码语言:javascript
运行
复制
<NavMenu donothing={null} params={null} selectedIndex={0} select={ null } />

有人能告诉我我做错了什么吗?目前,我只是尝试在列表的顶部输出选定的索引。当用户单击列表中的第一项或第二项时,我希望所选的索引文本将相应更新。

问题:

  1. 是否正确地使用布局和正确的导航栏实现导航条结构?
  2. 我怎么才能让它起作用?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-07-12 21:48:39

这篇文章有点老了,但我想我会分享我的经验,以防别人遇到这种情况。我为同样的问题奋斗了两天,终于开始工作了。

对我来说,这似乎归结为两件事。首先,我改变了:

代码语言:javascript
运行
复制
export class NavMenu extends React.Component<NavMenuProps, null> { 

并去掉了“导出”关键字:

代码语言:javascript
运行
复制
class NavMenu extends React.Component<NavMenuProps, null> { 

我猜想,在导出关键字就位后,当您在另一个组件中“导入NavMenu”时,它将捕获实际的NavMenu类,而不是封装在Redux的Connect对象中的类。

而且,在标记中包含NavMenu的NavMenu文件中,显然导入语句出错了。我把它改成:

代码语言:javascript
运行
复制
import NavMenu from './NavMenu';

所有这些现在都适用于我。如果我的回答有点混乱或不准确,很抱歉。我是个新手,也不是什么javascript专家,所以请不要激怒我.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42751912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档