【发布时间】:2016-12-03 05:14:53
【问题描述】:
我是 React 新手,我已经能够让 react-router、react-redux 和 Immutable 一起工作。在我的一个组件文件中,我的 React 组件中有一个 handleItemClick() 方法,它按预期工作:
mortgage.js
import import React from "react";
import { connect } from 'react-redux';
import { changeActiveMenu } from '../actions/action';
class Mortgage extends React.Component {
constructor(props) {
super(props);
this.handleItemClick = this.handleItemClick.bind(this);
}
handleItemClick (e, { name }) { //name reflects the routeName
const activeItemProp = this.props.app.get('primaryMenuActiveItem');
this.props.changeActiveMenu("test1", "test2");
}
render() {
console.log("Route Name=[" +this.props.route.name +"]");
console.log(this.props.app);
const activeItemProp = this.props.app.get('primaryMenuActiveItem');
return (
<div>
<h4>{activeItemProp}</h4>
<button onClick={this.handleItemClick}>Send Action</button>
</div>
)
}
}
export default connect(
state => ({ app: state.get('app'),routing: state.get('routing') }),
{ changeActiveMenu }
)(Mortgage)
我的动作很简单:
action.js
export function changeActiveMenu(activeItem) {
return {
type: 'CHANGE_MENU',
payload: {
activeItem: activeItem
}
};
}
这是我的应用程序缩减器:
app_reducer.js
import Immutable from 'immutable';
const initialState = Immutable.Map({
primaryMenuActiveItem: "overviewPrimary",
secondaryMenuActiveItem: 'home'
});
export default (state = initialState, action) => {
console.log("action.type=[" +action.type +"]");
if (action.type === "CHANGE_MENU") { //use the change primaryMenuActiveItem and secondaryMenuActiveItem
if(action.payload.activeItem.indexOf('Primary') >= 0) { //dealing with primaryMenu
return state.merge({ //returning new state
primaryMenuActiveItem: action.payload.activeItem
})
}
else { //dealing with secondaryMenu
return state.merge({ //returning new state
secondaryMenuActiveItem: action.payload.activeItem
})
}
}
return state; //returns initial state
}
我从我的 app reducer 中的 console.log() 获得了预期的 action.type 值 "CHANGE_MENU":
action.type=[CHANGE_MENU]
我的问题是当我尝试在我的 React 类中实现相同的代码时(main.js 中的第 6 行)。我从上面利用相同的操作和应用程序缩减程序文件。在我的 React 类中,它是我的 Main 组件的一部分:
main.js
import React from "react";
import { connect } from 'react-redux';
import { changeActiveMenu } from '../../actions/action';
const PrimaryMenu = React.createClass({
handleItemClick (e, { name }) { //name reflects the routeName
changeActiveMenu("primary", name);
},
renderMenu(menu) {
//use primaryMenuActiveItem and secondaryMenuActiveItem
const routingProp = this.props.routing.get('locationBeforeTransitions').get('pathname');
console.log(routingProp);
const primaryMenuActiveItem = routingProp === "/" ? 'overviewPrimary' : routingProp;
console.log(primaryMenuActiveItem);
return (
<div id="primary-menu">
<Menu pointing>
<Link to={menu[0].routeName}><Menu.Item name={menu[0].routeName} active={primaryMenuActiveItem === menu[0].routeName} onClick={this.handleItemClick}>{menu[0].menuName}</Menu.Item></Link>
</Menu>
</div>
)
},
render() {
const menu = this.props.data;
return (
<div id="menu">
{this.renderMenu(menu)}
</div>
)
}
});
class Main extends React.Component {
render() {
const {routing} = this.props;
return (
<div id="main">
<PrimaryMenu app = {this.props.app } routing = {this.props.routing } data={primaryMenuList}/>
{this.props.children}
</div>
)
}
}
export default connect(
state => ({ app: state.get('app'),routing: state.get('routing') }),
{ changeActiveMenu }
)(Main)
现在我的action.type如下:
action.type=[@@router/LOCATION_CHANGE]
我知道这与React class vs. component 有关。有任何想法吗?感谢所有帮助。
【问题讨论】:
标签: reactjs react-router react-redux immutable.js