如何解决Reactjs:道具类型失败:发布新数据时无法从道具中获取值
我正在开发一个社交应用程序,该应用程序应该让用户发布尖叫声,喜欢和发表评论。我面临的问题发布新的尖叫声时我没有获得props的值,但是重新加载页面后,一切工作正常。
这是 home.js ,我将从那里向Scream.js发送道具:-
class home extends Component {
componentDidMount() {
this.props.getScreams();
}
render() {
const { screams,loading } = this.props.data;
let recentScreamsMarkup = !loading ? (
screams.map((scream) => <Scream key={scream.screamId} scream={scream} />)
) : (
<ScreamSkeleton />
);
return (
<Grid container spacing={10}>
<Grid item sm={8} xs={12}>
{recentScreamsMarkup}
</Grid>
<Grid item sm={4} xs={12}>
<Profile />
</Grid>
</Grid>
);
}
}
home.propTypes = {
getScreams: PropTypes.func.isrequired,data: PropTypes.object.isrequired,};
const mapStatetoProps = (state) => ({
data: state.data,});
export default connect(mapStatetoProps,{ getScreams })(home);
这是 Scream.js ,它正在接收道具并在屏幕上显示尖叫声:-
class Scream extends Component {
render() {
dayjs.extend(relativeTime);
const {
classes,scream: {
body,createdAt,userImage,userHandle,screamId,likeCount,commentCount,},user: {
authenticated,credentials: { handle },} = this.props;
const deleteButton =
authenticated && userHandle === handle ? (
<DeleteScream screamId={screamId} />
) : null;
console.log("Image",{ userImage });
console.log("Handle",{ userHandle });
console.log("Body",{ body });
return (
<Card className={classes.card}>
<CardMedia
image={userImage}
title="Profile image"
className={classes.image}
/>
<CardContent className={classes.content}>
<Typography
variant="h5"
component={Link}
to={`/users/${userHandle}`}
color="primary"
>
{userHandle}
</Typography>
{deleteButton}
<Typography variant="body2" color="textSecondary">
{dayjs(createdAt).fromNow()}
</Typography>
<Typography variant="body1">{body}</Typography>
</CardContent>
</Card>
);
}
}
Scream.propTypes = {
user: PropTypes.object.isrequired,scream: PropTypes.object.isrequired,classes: PropTypes.object.isrequired,openDialog: PropTypes.bool,};
const mapStatetoProps = (state) => ({
user: state.user,});
export default connect(mapStatetoProps)(withStyles(styles)(Scream));
这是 PostScream.js ,用于发布尖叫:-
class PostScream extends Component {
state = {
open: false,body: "",errors: {},};
UNSAFE_componentwillReceiveProps(nextProps) {
if (nextProps.UI.errors) {
this.setState({
errors: nextProps.UI.errors,});
}
if (!nextProps.UI.errors && !nextProps.UI.loading) {
this.setState({
body: "",open: false,});
}
}
handleOpen = () => {
this.setState({ open: true });
};
handleClose = () => {
this.props.clearErrors();
this.setState({ open: false,errors: {} });
};
handleChange = (event) => {
this.setState({ [event.target.name]: event.target.value });
};
handleSubmit = (event) => {
event.preventDefault();
this.props.postScream({ body: this.state.body });
};
render() {
const { errors } = this.state;
const {
classes,UI: { loading },} = this.props;
return (
<Fragment>
<MyButton onClick={this.handleOpen} tip="Post a Scream!">
<AddIcon />
</MyButton>
<Dialog
open={this.state.open}
onClose={this.handleClose}
fullWidth
maxWidth="sm"
>
<MyButton
tip="Close"
onClick={this.handleClose}
tipClassName={classes.closeButton}
>
<CloseIcon />
</MyButton>
<DialogTitle> Post a new Scream </DialogTitle>
<DialogContent>
<form onSubmit={this.handleSubmit}>
<TextField
name="body"
type="text"
label="SCREAM!"
multiline
rows="3"
placeholder="What's on your mind!"
error={errors.body ? true : false}
helperText={errors.body}
className={classes.TextField}
onChange={this.handleChange}
fullWidth
/>
<Button
type="submit"
variant="contained"
color="primary"
className={classes.submitButton}
disabled={loading}
>
Submit
{loading && (
<CircularProgress
size={30}
className={classes.progressspinner}
/>
)}
</Button>
</form>
</DialogContent>
</Dialog>
</Fragment>
);
}
}
PostScream.propTypes = {
postScream: PropTypes.func.isrequired,clearErrors: PropTypes.func.isrequired,UI: PropTypes.object.isrequired,};
const mapStatetoProps = (state) => ({
UI: state.UI,{ postScream,clearErrors })(
withStyles(styles)(PostScream)
);
正如您在Scream.js中看到的那样,我正在控制台中打印props的值,但未定义,但是在重新加载后我得到了值。
发布新的尖叫声时的图像:- While Posting a new Scream
重新加载后的图片:- After Reloading
解决方法
我不使用基于类的组件,大麦则使用。您能告诉我您在尖叫时如何更新吗? 也删除/裁剪您的照片!不要显示您的私人数据。我强烈建议您尝试一下功能组件。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。