我正在开发一个社交应用程序,用户应该在其中发布尖叫声、点赞和评论。我面临的问题是,当我发布新的尖叫声时,我没有获得道具价值,但在重新加载页面后,一切正常。
这是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,
errors: {},
});
}
}
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,
});
export default connect(mapStateToProps, { postScream, clearErrors })(
withStyles(styles)(PostScream)
);
正如您在 Scream.js 中看到的那样,我正在控制台中打印道具的值,但在重新加载后我得到了未定义的值。
发布新尖叫时的图像:- 发布新尖叫时
重新加载后的图像:- 重新加载后