【发布时间】:2018-10-29 17:35:24
【问题描述】:
我正在尝试实现来自https://react.semantic-ui.com/modules/sidebar/#types-sidebar 的侧边栏功能。但我仍然收到未定义的“可见”错误。我想在禁用时将映射状态传递给从索引到按钮上的 navbar.js 的“可见”道具。
所以我创建了一个reducer 'reducers/App.js':
import {
HIDE_CLICK,
SHOW_CLICK,
HIDE_SIDE_BAR,
} from "../actions/app";
const initialState = {
visible: false
};
const appReducer = (state = initialState, {type}) => {
switch(type) {
case HIDE_CLICK:
return {
...state,
visible: false,
}
case SHOW_CLICK:
return{
...state,
visible: true,
}
case HIDE_SIDE_BAR:
return {
...state,
visible: false,
}
};
}
export default appReducer;
然后它的动作'action/App.js':
export const HIDE_CLICK = "HIDE_CLICK";
export const SHOW_CLICK = "SHOW_CLICK";
export const HIDE_SIDE_BAR = "HIDE_SIDE_BAR";
export const handleHideClick = () => ({
type: HIDE_CLICK,
})
export const handleShowClick = () => ({
type: SHOW_CLICK,
})
export const handleSideBarHide = () => ({
type: HIDE_SIDE_BAR,
})
现在我的组件包含:'NavBar/index.js'
import { connect } from "react-redux";
import NavBar from "./NavBar";
import { handleHideClick, handleShowClick, handleSideBarHide } from "../../redux/actions/app";
/* istanbul ignore next */
const mapStateToProps = state => ({
isAuthenticated: state.authentication.authenticated,
visible: state.app.visible
});
const mapDispatchToProps = (dispatch) => {
return{
handleHideClick: () => dispatch(handleHideClick),
handleShowClick: () => dispatch(handleShowClick),
handleSideBarHide: () => dispatch(handleSideBarHide)
}
};
export default connect(
mapStateToProps,
mapDispatchToProps,
)(NavBar);
还有我的“NavBar/NavBar.js”
import React from "react";
import PropTypes from "prop-types";
import { Link } from "react-router-dom";
import { Container, Menu, Button, Sidebar,Segment, Icon, Header, Image } from "semantic-ui-react";
export const ShowSideBar = ({
handleShowClick,
handleHideClick,
handleSideBarHide
}) => (
<div>
<Button.Group>
<Button disabled={visible} onClick={handleShowClick}>
Show sidebar
</Button>
<Button disabled={!visible} onClick={handleHideClick}>
Hide sidebar
</Button>
</Button.Group>
<Sidebar.Pushable as={Segment}>
<Sidebar
as={Menu}
animation='overlay'
icon='labeled'
inverted
onHide={handleSideBarHide}
vertical
visible={visible}
width='thin'
>
...
)
最后在我的根 reducer 上定义了 app reducer:
import { combineReducers } from "redux";
import authentication from "./authentication";
import app from "./app";
const rootReducer = combineReducers({
authentication,
app
});
export default rootReducer;
【问题讨论】:
-
尝试更改操作如下返回 JSON export const handleHideClick = () => (return { type: HIDE_CLICK, })
-
我需要使用 redux 操作和 reducer 来遵循当前格式。
-
疯狂猜测:用
disabled={this.props.visible}代替disabled={visible} -
我现在收到此错误:
Failed to compile ./src/redux/reducers/app.js Line 28: Expected an assignment or function call and instead saw an expression no-unused-expressions Search for the keywords to learn more about each error.
标签: reactjs redux react-redux