【发布时间】:2019-07-18 00:05:12
【问题描述】:
我正在使用带有 redux thunk 的反应。我创建了一个动作,两个减速器(一个父母和一个孩子)和一个商店。现在我将 reactstrap 用于导航栏,其中 this.state 方法用于切换导航栏,但我想 toggle 使用 调度动作 和 设置和从存储中获取状态。以下是我的代码:
/* App.js */
import React from "react";
import { connect } from 'react-redux';
import {
Collapse,
Navbar,
NavbarToggler,
NavbarBrand,
Nav,
NavItem,
NavLink } from 'reactstrap';
import { simpleAction } from './actions/simpleAction';
const mapStateToProps = state => ({
...state
})
const mapDispatchToProps = dispatch => ({
simpleAction: () => dispatch(simpleAction())
})
class App extends React.Component {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
isOpen: false
};
}
toggle() {
this.setState({
isOpen: !this.state.isOpen
});
}
simpleAction = (event) => {
this.props.simpleAction();
}
render() {
return (
<div>
<Navbar color="light" light expand="md">
<NavbarBrand href="/">reactstrap</NavbarBrand>
<NavbarToggler onClick={this.toggle} />
<Collapse isOpen={this.state.isOpen} navbar>
<Nav className="ml-auto" navbar>
<NavItem>
<NavLink href="/components/">Components</NavLink>
</NavItem>
<NavItem>
<NavLink href="https://github.com/reactstrap/reactstrap">GitHub</NavLink>
</NavItem>
</Nav>
</Collapse>
</Navbar>
</div>
);
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
/* simpleAction.js */
export const simpleAction = () => dispatch => {
dispatch({
type: 'SIMPLE_ACTION',
payload: 'result_of_simple_action'
})
}
/* simpleReducer.js */
export default (state = {}, action) => {
switch (action.type) {
case 'SIMPLE_ACTION':
return {
result: action.payload
}
default:
return state
}
}
【问题讨论】:
标签: javascript reactjs redux react-redux