【问题标题】:Pass multiple props to component in react-materialui将多个道具传递给 react-material ui 中的组件
【发布时间】:2018-05-09 18:37:22
【问题描述】:

我正在尝试使用 PropTypes 将样式和文本传递给 reactjs 组件,但我收到 TypeError: Cannot read property 'header' of undefined 错误,它指出了错误到 {typo.header}

无法将 2 个不同的道具传递给反应组件。我确定我没有正确传递道具,我正在学习 reactjs 并且不确定如何正确地做到这一点。有人可以指导我如何做到这一点。

App.js:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Card, { CardActions, CardContent } from 'material-ui/Card';
import Typography from 'material-ui/Typography';

import './App.css';
import { AppStyle } from "./AppStyle";

import { Typos } from "./AppTypo";

function SurveyApp(props) {
  const { classes, typo } = props;

  return (
    <div className={classes.wrapper}>
      <div className={classes._header}>
      </div>
      <div className={classes._body}>
        <div className={classes.__formContainer}>
        <Card className={classes.card}>
          <CardContent>
            <Typography>
              {typo.header}
            </Typography>
            <Typography>

            </Typography>
          </CardContent>
        </Card>
        </div>
      </div>
    </div>
  );
}

SurveyApp.propTypes = {
  classes: PropTypes.object.isRequired,
  typo: PropTypes.object.isRequired
}
export default withStyles(AppStyle, Typos)(SurveyApp);

AppStyle.js:

export const AppStyle = {
  wrapper: {
    width: '100%',
    height: '100%',
    position: 'relative'
  },
  _header: {
    width: '100%',
    height: 100,
    backgroundColor: '#295e94',
    position: 'relative'
  },
  _body: {
    width: '100%;',
    height: 'auto',
    minHeight: 'calc(100% - 100px)',
    position: 'relative',
    display: 'flex',
    flexDirection: 'row',
    alignContent: 'center',
    alignItems: 'start',
    justifyContent: 'center'
  },
  __formContainer: {
    width: 750,
    position: 'absolute',
    top: -30
  },
  '@media (max-width: 480px)': {
    __formContainer: {
      width: '100%',
      position: 'absolute',
      top: -30
    }
  }
}

AppTypo.js:

export const Typos = {
  header: "User Experience",
  subHeader: "This is intended to improve your experience."
}

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    您没有正确提供道具。

    export default withStyles(AppStyle, Typos)(SurveyApp);

    Typos 的作用不同。

    除此之外,header 有两个不同的定义:header_header。您通过withStyles 传递的样式是_header,来自AppStyle。那是你应该使用的。

    您不应该尝试通过withStyles 传递非样式信息,这看起来就像您尝试使用Typos 所做的那样。那些需要从父组件进来,或者既然这是父组件,就使用导入的Typos.header

    我建议您read more on props in general 更好地了解它们的用途以及如何使用它们。

    将来,您可能会发现使用React's Context 处理样式更好。

    【讨论】:

    • 对不起,我没有找到你
    • import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import registerServiceWorker from './registerServiceWorker'; ReactDOM.render(&lt;App /&gt;, document.getElementById('root')); registerServiceWorker();
    • 其实我在关注material-ui-next.com/demos/cards例子
    • 我没有使用&lt;SurveyApp typo={Typos}/&gt;我正在使用PropTypes
    猜你喜欢
    • 2018-11-24
    • 2020-08-12
    • 2019-03-29
    • 2021-07-18
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2020-05-08
    • 2020-12-21
    相关资源
    最近更新 更多