admin管理员组文章数量:1327988
I have a react application that looks like this: I have a button when it's clicked it will open a popup with a ticket to be printed, the click handler is passed from a parent ponent to the button and I want the same process to be done inside the parent ponent
export default class ParentComponent extends Component {
openWindow = id => {
window.open('/service/ticket/' + id + '/print')
}
submitPayment = () => {
sendPayment(this.state.rawCommand.id, updatedCommand.payment)
.then(payment => {
if (payment.needed > 0) {
toastr.success(dictionnary.acceptedPayment)
} else {
toastr.success(dictionnary.payee)
this.openWindow(updatedCommand.id)
}
}
render () {
<Printer printTicket={this.openWindow} />
}
}
and
const Printer = ({printTicket, id}) => {
const clickHandler = () => printTicket(id)
return (
<button onClick={clickHandler}>print</button>
)
}
The problem is when the button in the Printer ponent is clicked the window is opened as expected, but when the openWindow click handler is invoked from the parent ponent nothing is happening!
I inspected the code in chrome devtools and the function is invoked but the popup does not open.
Edit More explanation: 1- the button opens a popup with a ticket and print it 2- the user can print a ticket via this button 3- after submitting a payment the ticket should be printed automaticaly
I have a react application that looks like this: I have a button when it's clicked it will open a popup with a ticket to be printed, the click handler is passed from a parent ponent to the button and I want the same process to be done inside the parent ponent
export default class ParentComponent extends Component {
openWindow = id => {
window.open('/service/ticket/' + id + '/print')
}
submitPayment = () => {
sendPayment(this.state.rawCommand.id, updatedCommand.payment)
.then(payment => {
if (payment.needed > 0) {
toastr.success(dictionnary.acceptedPayment)
} else {
toastr.success(dictionnary.payee)
this.openWindow(updatedCommand.id)
}
}
render () {
<Printer printTicket={this.openWindow} />
}
}
and
const Printer = ({printTicket, id}) => {
const clickHandler = () => printTicket(id)
return (
<button onClick={clickHandler}>print</button>
)
}
The problem is when the button in the Printer ponent is clicked the window is opened as expected, but when the openWindow click handler is invoked from the parent ponent nothing is happening!
I inspected the code in chrome devtools and the function is invoked but the popup does not open.
Edit More explanation: 1- the button opens a popup with a ticket and print it 2- the user can print a ticket via this button 3- after submitting a payment the ticket should be printed automaticaly
Share Improve this question edited Jun 3, 2021 at 8:49 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 21, 2017 at 9:23 zied hajsalahzied hajsalah 5761 gold badge6 silver badges17 bronze badges 8-
I'd expect to see a ReferenceError for
mandId
on the console with that code. – Quentin Commented Apr 21, 2017 at 9:24 - it's a typo in the question only thank you! – zied hajsalah Commented Apr 21, 2017 at 9:26
-
How are you invoking
submitPayment
? – Pineda Commented Apr 21, 2017 at 9:28 - submitPayment is passed as props to another ponent and when it's invoked it does an ajax call which returns a Promise, and when the Promise is resolved it calls this.openWindow. – zied hajsalah Commented Apr 21, 2017 at 9:31
- 'but when the openWindow click handler is invoked from the parent ponent nothing is happening', where is the code that invoked it within the parent and not a child ponent? – Pineda Commented Apr 21, 2017 at 9:46
1 Answer
Reset to default 7I found the solution it's about google chrome: it blocks the popup!!!
本文标签: javascriptwindowopen() don39t work in react applicationStack Overflow
版权声明:本文标题:javascript - window.open() don't work in react application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742250534a2440672.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论