繁体   English   中英

反应,从组件处理模态

[英]React, handle modal from component

如何从模态中捕获对某些按钮的点击,以向调用模态的组件返回 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 });

您可以传递要在模态内执行的handler

const Modal = ({ callback }) =>{
    const handleClick = arg => callback(arg)

    return(
        <div>
            <button  onClick={() => handleClick('button1')}>A</button>
             <button  onClick={() => handleClick('button2')}> B</button>
        </div>
    )
}

并期望在调用Modal的组件内接收此值

const TheOneWhoCalls = () =>{
    const onModalClick = arg => console.log(arg)

    return <Modal callback={onModalClick} />
}

您可以在父组件上创建一个函数,而在模态中,您只能使用它。 https://reactjs.org/docs/lifting-state-up.html#lifting-state-up

家长:

constructor () {
  this.state: {test: false}
}

setStateTest (value) {
  this.setState(value)
}

render () {
  return <modal handlerSetParentStateTest = {setStateTest}></modal>
}

模态:

// this will set the parent state
this.props.handlerSetParentStateTest(true); 

我想分享我的解决方案,我将来肯定会需要它。 它是@Dupocas 的实现

const Modal = ({ callback }) => {
    const handleClick = arg => callback(arg)

    return (
    <div>
        Wanna save?
        <Button id="btnCloseModal"      onClick={() => handleClick(0)}>No</Button>
        <Button id="btnGuardarConfirm"  onClick={() => handleClick(1)}>Sí</Button>
    </div>)
};

class TableDisplayReportRecord extends Component<Props, State> {

    constructor {...}

    handleValidate = async (row) => {

        const { scadaValidation } = this.props;

        const verify = await scadaValidation();

        if (verify.statusCode !== 200) {
            this.setState({openSimpleModal: true});

            const onModalClick = arg => {
                this.setState({openSimpleModal: false});
                //do nothing
                if (arg === 0) return false;
                //if user selected "Yes", call the function that I need
                else this.handleSubmitSave(row);

            };

            this.setState({contentSimpleModal:
                    <Modal
                        callback={onModalClick}
                    />
            })
        }
    }

    handleSubmitSave = async (row) => {...}

    ...

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM