console.log("resize--->", window.innerWidth);
this.resize();
https://codesandbox.io/s/material-demo-4gnt3
resize() {
if (window.innerWidth <= 760) {
console.log("resize--->", window.innerWidth);
return (
<div>
jkdfjkdfjkdf jdfkljkdfjkdfjk jkdfjkdfjkdfjk jkjkjkjk jkjkjkjk klklklkl
klklklk klklklkl klklklkl klklklkl klklklkl klklklkl klklklkl klklklkl
I am here
</div>
);
}
// this.setState({ hideNav: window.innerWidth <= 760 });
}
render() {
const { classes } = this.props;
const bull = <span className={classes.bullet}>•</span>;
{
this.resize();
}
}
答案 0 :(得分:0)
使用内部反应状态来控制显示如下元素:
resize() {
if (window.innerWidth <= 760) {
this.setState({ showDiv: true });
} else {
this.setState({ showDiv: false });
}
}
render() {
return (
<Card className={classes.card}>
{this.state.showDiv && <div>jkdfjkdfjkdf...</div>}
</Card>
);
}
答案 1 :(得分:0)
我稍微重构了您的示例,它对我有用。链接如下:
https://codesandbox.io/embed/material-demo-c7xzb
我所做的更改是:
div
中包装了您要返回的所有内容return
方法中(屏幕上不会显示其他任何内容)console.log
中的showDiv
,但这没关系