Here is what I tried, and details what I want to achieve, can someone help.
class ConfirmDialog extends React.Component {
callback(action){
alert(action)
}
render(){
return(
<div className='dialog'>
<div>confirm dialog</div>
<button onClick={() => this.callback('yes')}>Yes</button>
<button onClick={() => this.callback('no')}>No</button>
</div>
)
}
}
class Hello extends React.Component {
constructor(props){
super(props);
this.state = {
showDialog: false,
}
}
onButtonClick(params) {
//I want yes no callback here without loosing my previous params
//so i can use switch case here for yes / no action.
this.setState({showDialog: !this.state.showDialog})
}
render() {
return (
<div>
<button onClick={() => this.onButtonClick({test: 'test params'})}>
Click</button>
{
this.state.showDialog ?
<ConfirmDialog callback={this.onButtonClick}/> : null
}
</div>
)
}
}
ReactDOM.render(
<Hello />,
document.getElementById('container')
);
.dialog {
background: tomato;
width: 150px;
height: 150px;
margin: auto;
}
.dialog button {
display : inline-block;
text-align: center;
margin: 0 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
The on callback function should get which action I clicked and without losing the parameters. React JS - confirmation dialog with function callback with the action without loosing the previous parameter