Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

javascript - 反应,处理组件的模态(React, handle modal from component)

How can I catch the click on some buttom from a modal, to return true or false to the component that is calling the modal?(如何捕获模态中某些按钮的单击,以将true或false返回给调用模态的组件?)

handleSubmitSaveConfigurations = async (row) => { const { scadaValidation } = this.props; const result = await scadaValidation(11); if (result.statusCode !== 200) { // Opens the modal to ask if you really want to save this.setState({openSimpleModal: true}); this.setState({contentSimpleModal: this.warningModal()}); // Here I have to catch if the modal click yes or no. // In case yes, do nothing and continue with the code // But in case "no" returns false and stops } // If result.statusCode === 200 returns true return true; } warningModal = () => ( <div> Do you want to save? <Button id="btnClose" onClick={() => this.handleModalClickClose()}>No</Button> <Button id="btnSave" onClick={() => this.handleModalClickClose()}>Yes</Button> </div> ); handleModalClickClose = () => this.setState({ openSimpleModal: false });   ask by pmiranda translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could pass a handler to be executed inside your modal.(您可以传递一个要在模态内部执行的handler 。)

const Modal = ({ callback }) =>{ const handleClick = arg => callback(arg) return( <div> <button onClick={() => handleClick('button1')}>A</button> <button onClick={() => handleClick('button2')}> B</button> </div> ) } And expect to receive this value inside the component which is calling Modal(并期望在调用Modal的组件中接收此值) const TheOneWhoCalls = () =>{ const onModalClick = arg => console.log(arg) return <Modal callback={onModalClick} /> }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...