React无法反应未定义的属性路径

问题描述 投票:0回答:1

我正在构建一个高级订单组件,以在我的react应用中创建一个Idle Timeout功能。我有自动注销组件,正在将其包含在index.js文件中。但是,我遇到了无法读取路径的问题。我究竟做错了什么?

这是我的HOC组件:

import IdleTimer from "react-idle-timer";
import React, { Component } from 'react'
import PropTypes from 'prop-types';
import { Switch, Route } from 'react-router-dom'
import DefaultLayout from "../containers/DefaultLayout";


export default function HOC (WrappedComponent) {
 return class extends Component{
      constructor(props) {
          super(props);

          this.state = {
              timeout:      1000*5,
              showModal:    false,
              userLoggedIn: false,
              isTimedOut:   false
          };

          this.idleTimer = null;
          this.onAction = this._onAction.bind(this);
          this.onActive = this._onActive.bind(this);
          this.onIdle = this._onIdle.bind(this);

      }

      _onAction(e){
          console.log('User did something', e);
          this.setState({isTimedOut: false})
      }

      _onActive(e){
          console.log('user is active', e);
          this.setState({isTimedOut: false})
      }

      _onIdle(e){
          console.log('user is idle', e);
          const isTimedOut = this.state.isTimedOut;
          if (isTimedOut){
              this.props.history.push('/')
          }else {
              this.setState({showModal: true});
              this.idleTimer.reset();
              this.setState({isTimedOut: true})
          }
      }

    render() {
        const { match } = this.props;
        return (
            <WrappedComponent>
                <IdleTimer
                    ref={ref => {this.idleTimer = ref}}
                    element={document}
                    onActive={this.onActive}
                    onIdle={this.onIdle}
                    onAction={this.onAction}
                    debounce={250}
                    timeout={this.state.timeout} />

                    <div className="">
                        <Switch>
                            <Route
                               exact path={`${match.path}/sales-analysis/dashboard`}
                                render={(props) => <DefaultLayout {...props} /> }/>
                            />
                        </Switch>
                    </div>
            </WrappedComponent>
        )
    }

 }
}
    HOC.propTypes = {
        match: PropTypes.any.isRequired,
        history: PropTypes.func.isRequired
    };

这里是index.js文件,其中包含我要监控的路由

class AppEntry extends React.Component {
  componentDidMount() {
    window.addEventListener("appNotifyUpdate", this.appNotifyUpdate);
    window.addEventListener("appUpdate", this.appUpdate);
    window.addEventListener("offline", function(e) {
      store.dispatch(setOffline(true));
    });
    window.addEventListener("online", function(e) {
      store.dispatch(setOffline(false));
    });
  }

  componentWillUnmount() {
    window.removeEventListener("appNotifyUpdate", this.appNotifyUpdate);
    window.removeEventListener("appUpdate", this.appUpdate);
    window.removeEventListener("offline", function(e) {
      store.dispatch(setOffline(true));
    });
    window.removeEventListener("online", function(e) {
      store.dispatch(setOffline(false));
    });
  }

  appNotifyUpdate = e => {
    store.dispatch(setAppUpdateBar(true));
  };

  appUpdate = e => {
    store.dispatch(setAppUpdateBar(false));
  };

  render() {
    return (
      <Provider store={this.props.store}>
        <PersistGate loading={<div />} persistor={this.props.persistor}>
          <BrowserRouter>
            <div id="browserRouter">
              <Route exact path="/" component={LoginContainer} key="login" />
              <Route
                path="/change-password"
                component={LoginContainer}
                key="change-password"
              />
              <Route path="/logout" component={Logout} key="logout" />
              <DefaultLayout
                path="/sales-analysis/dashboard"
                component={HOC(DashboardContainer)}
              />
              <DefaultLayout
                path="/sales-analysis/active-clients"
                component={ActiveClientsContainer}
              />
              <DefaultLayout
                path="/sales-analysis/activity-reports"
                component={ActivityReportsContainer}
              />
              <DefaultLayout
                path="/sales-analysis/segments"
                component={SegmentsContainer}
              />
              <DefaultLayout path="/prospects" component={ProspectsContainer} />
              <DefaultLayout
                path="/preferences/general"
                component={PreferenceGeneral}
              />
              <DefaultLayout
                path="/preferences/account-manager"
                component={PreferenceAccountManager}
              />
              <DefaultLayout
                path="/preferences/flex-fields"
                component={PreferenceFlexFields}
              />
              <DefaultLayout
                path="/preferences/oem-manager"
                component={PreferenceOEMManager}
              />
              <DefaultLayout
                path="/preferences/users"
                component={PreferenceUsers}
              />
              <DefaultLayout
                path="/preferences/group-users"
                component={PreferenceGroupUsers}
              />
            </div>
          </BrowserRouter>
        </PersistGate>
      </Provider>
    );
  }
}
AppEntry = HOC(AppEntry);

我得到的确切错误是这个

TypeError: Cannot read property 'path' of undefined
render
./src/components/AutoLogout.js:65:52
  62 | <div className="">
  63 |     <Switch>
  64 |         <Route
> 65 |            exact path={`${match.path}/sales-analysis/dashboard`}
     |            render={(props) => <DefaultLayout {...props} /> }/>
  67 |         />
  68 |     </Switch>
javascript reactjs redux session-timeout higher-order-components
1个回答
0
投票

这是由于您没有将道具传递给您的HoC。

尝试,(例如):

//Imports
import HOC from 'path/to/my/hoc';

//...code

const MyHoc = HOC(DashboardContainer);

//Main class
class AppEntry extends React.Component {

//...code

// In the render method take advantage of render prop
render() {
  return (
    //...code
    <Route exact path="/my-path" render={(props) => <MyHoc {...props} />} />
© www.soinside.com 2019 - 2024. All rights reserved.