【发布时间】: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