如何解决在ComponentDidUpdate中打印一个组件反应
我ḿ尝试在渲染后打印组件,但“ document.getElementById”告诉我下一个错误:
这里是我的代码:
import React,{ Component } from "react";
import { connect } from "react-redux";
import "../../styles/module/ticket.css";
import { clearSale } from "../../actions/Operations";
import { UNKNOWN_EMPLOYEE } from "constants/ActionTypes";
class AttrTicket extends Component {
componentDidMount() {
const printableElements = document.getElementById("formToPrint").innerHTML;
const orderHTML =
"<html><head><title></title></head><body>" +
printableElements +
"</body></html>";
const oldPage = document.body.innerHTML;
document.body.innerHTML = orderHTML;
window.print();
document.body.innerHTML = oldPage;
localStorage["sale_employee"] = UNKNOWN_EMPLOYEE;
this.props.clearSale();
window.location.replace("");
}
render() {
return (
<div id="formToPrint" class="ticket">
<h1>Hello</h1>
<h1>Hola</h1>
<h1>Bye</h1>
<h1>Adios</h1>
<h1>Smileee</h1>
</div>
);
}
}
export default connect(mapStateToProps,{ clearSale })(AttrTicket);
如果您能帮助我,我将不胜感激
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。