【问题标题】:React-Redux error: 'visible' is undefinedReact-Redux 错误:“可见”未定义
【发布时间】: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


【解决方案1】:

你需要列出你需要的所有道具,或者不要解构它们:

export const ShowSideBar = ({
  handleShowClick,
  handleHideClick,
  handleSideBarHide,
  visible
}) => (
  ....
)

或export const ShowSideBar = (props) =&gt; (...) 并使用props. 访问所有内容

【讨论】:

  • 我收到了这个新错误:I'm getting this error now : 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.
  • 我的错误,我忘记了switch case中默认的return语句
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 2018-06-01
  • 2020-06-21
  • 2019-01-15
  • 2019-10-11
相关资源
最近更新 更多