首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React类继承

React类继承
EN

Stack Overflow用户
提问于 2021-08-31 09:17:28
回答 1查看 66关注 0票数 0

我遇到了一个从React JS中的前一个组件继承信息的组件问题。在下面的例子中,我试图从props中登录的用户那里获取信息。但是,在组装仪表板页面(ComponentDidMount)时,我无法加载此信息。

componente/BaseDashboard.js

代码语言:javascript
复制
import React from 'react'; import Header from
'../../components/Header';

import { connect } from 'react-redux'; import * as actions from
'../../store/actions'; import Footer from '../../components/Footer';

class BaseDashboard extends React.Component {
    state = {
        statusMenu: true
    }

    alertMenu() {
        this.setState({ statusMenu: !this.state.statusMenu })
    }

    alertMenu = this.alertMenu.bind(this);

    render() {
        
        return (
            <>
                <div className="wrapper">

                    <Header handleLogout={this.props.handleLogout} userData={this.props} alertMenu={this.alertMenu} />
                    <div className="content-wrapper">
                        {this.props.children}
                    </div>


                    <Footer handleLogout={this.props.handleLogout} userData={this.props} alertMenu={this.alertMenu} />
                </div>
            </>
        );
    } }

export default connect(null, actions)(BaseDashboard);

componente/index.js

代码语言:javascript
复制
import React from 'react'; import BaseDashboard from
'./BaseDashboard'; import { connect } from 'react-redux'; import * as
actions from '../../store/actions';

const baseDashboard = Component => {
    class ComponentBaseDashboard extends React.Component {


        componentDidMount() {
            const { authorized, getViewUser, history } = this.props;
            getViewUser();

            if (!authorized) {
                return history.replace("/");
            }
        }

        componentDidUpdate(nextProps) {
            const { authorized, history } = this.props;
            if (!nextProps.authorized || !authorized) {
                return history.replace("/");
            }
        }

        render() {
            return (
                <BaseDashboard>
                    <Component {...this.props} />
                </BaseDashboard>
            );
        }
    }

    const mapStateToProps = state => ({
        authorized: state.auth.authorized,
        user: state.auth.user
    });

    return connect(mapStateToProps, actions)(ComponentBaseDashboard); }

export default baseDashboard;

在此方法中,当我为this.props.user pages/Dashboard.js运行console.log时,出现消息"TypeError: Cannot read property of undefined“

代码语言:javascript
复制
componentDidMount() {
   const { user } = this.props;
   console.log(this.props);
   this.setState({ user: user })
}

我在我的路线中以这种方式调用的仪表板页面

EN

回答 1

Stack Overflow用户

发布于 2021-08-31 09:53:06

您说在未知组件中未定义this.props (您发布了2个日志this.props,但没有日志日志)。

下面是一个minimum example示例,其中没有显示代码应该正常工作的所有不相关代码:

代码语言:javascript
复制
const { Provider, connect } = ReactRedux;
const { createStore, applyMiddleware, compose } = Redux;

const initialState = { value: 22 };
const reducer = (state, { type, payload }) => {
  return state;
};

class BaseDashboard extends React.Component {
  componentDidMount() {
    console.log(
      'base props no problem:',
      Boolean(this.props)
    );
  }
  render() {
    return <div>hello world</div>;
  }
}
const ConnectedBaseDashboard = connect()(BaseDashboard);
const baseDashboard = (Component) => {
  class ComponentBaseDashboard extends React.Component {
    componentDidMount() {
      console.log('props no problem:', this.props);
    }
    render() {
      return (
        <BaseDashboard>
          <Component {...this.props} />
        </BaseDashboard>
      );
    }
  }
  const mapStateToProps = (state) => state;
  return connect(
    mapStateToProps,
    {}
  )(ComponentBaseDashboard);
};

//creating store with redux dev tools
const composeEnhancers =
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
  reducer,
  initialState,
  composeEnhancers(
    applyMiddleware(
      () => (next) => (action) => next(action)
    )
  )
);
const App = baseDashboard(ConnectedBaseDashboard);

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.5/redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/7.2.0/react-redux.min.js"></script>

<div id="root"></div>

也许您可以使用上面的代码片段来演示您在问题中遇到的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68996046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档