新的这些技术,并有一个关于我的单页应用程序的问题。我从这个模板中的reactredux示例开始:
我的SPA的根组件是布局。NavMenu直接位于布局和RightNavMenu对象之下。RightNavMenu根据NavMenu的状态变化,因此我在布局级别上设置了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):从‘redux’导入{ Action,Reducer };
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 } />有人能告诉我我做错了什么吗?目前,我只是尝试在列表的顶部输出选定的索引。当用户单击列表中的第一项或第二项时,我希望所选的索引文本将相应更新。
问题:
谢谢。
发布于 2017-07-12 21:48:39
这篇文章有点老了,但我想我会分享我的经验,以防别人遇到这种情况。我为同样的问题奋斗了两天,终于开始工作了。
对我来说,这似乎归结为两件事。首先,我改变了:
export class NavMenu extends React.Component<NavMenuProps, null> { 并去掉了“导出”关键字:
class NavMenu extends React.Component<NavMenuProps, null> { 我猜想,在导出关键字就位后,当您在另一个组件中“导入NavMenu”时,它将捕获实际的NavMenu类,而不是封装在Redux的Connect对象中的类。
而且,在标记中包含NavMenu的NavMenu文件中,显然导入语句出错了。我把它改成:
import NavMenu from './NavMenu';所有这些现在都适用于我。如果我的回答有点混乱或不准确,很抱歉。我是个新手,也不是什么javascript专家,所以请不要激怒我.
https://stackoverflow.com/questions/42751912
复制相似问题