如何在React Native中关闭类似于模态框的组件?

huangapple go评论82阅读模式
英文:

How to close a component like modal with React Native?

问题

Index.js

import TelaAdd from './AddHospital/Modal';

class Index extends Component {

    constructor(props) {
        super(props);
        this.state = {
            listaFirebase: [],
            showModal: false,
            search: ''
        };
    }

    openModal(value) {
        this.setState({
            showModal: value
        });
    }

    render() {
        return (
            <Container>
                <TouchableOpacity style={styles.addButton} activeOpacity={0.7} onPress={() => this.openModal(true)}>
                    <Icon name="plus" size={20} color='#f2f2f2' />
                </TouchableOpacity>

                <TelaAdd
                    show={this.state.showModal}
                    setShow={(value) => this.openModal(value)} />
            </Container>
        );
    }
}

Modal.js

export default ({ show, setShow }) => {

    const onCancel = () => {
        setShow(false);
    }

    return (
        <ModalHead transparent={true} visible={show} animationType='slide'>
            <ModalArea>
                <ModalBody>
                    <Voltar onPress={() => onCancel()}>
                        <IconVoltar width="30" height="30" fill="#000" />
                    </Voltar>
                </ModalBody>
            </ModalArea>
        </ModalHead>
    );
}
英文:

I created a class Index that call a component "TelaAdd" like a modal. The Modal open but i don't know how to close that. I tried to use setShow but doesn't work.
In the Modal has a Icon that was closing it before, but i had to change code creating a class component and it stopped working. I'd like to press the icon "IconVoltar" in Modal and close that.

Index.js

import  TelaAdd  from  &#39; ./AddHospital/Modal &#39;


class Index extends Component 

{

constructor (props) {

    super(props)   
    this.state = {
        listaFirebase: [],
        showModal: false,
        search: &#39;&#39;              
    }}

   openModal(value) {

     this.setState({
        showModal: value })}


 render () {    
    return (            
        &lt;Container&gt;
          &lt;TouchableOpacity style={styles.addButton}
            activeOpacity={0.7}
            onPress={() =&gt; this.openModal(true)}&gt;

                &lt;Icon name=&quot;plus&quot; size={20} color=&#39;#f2f2f2&#39; /&gt;
          &lt;/TouchableOpacity&gt;

          &lt;TelaAdd               
                show={this.state.showModal}
                setShow(that&#39;s right?) /&gt;
        &lt;/Container&gt;

The other file
Modal.js

export default ({ show, setShow })  =&gt;  {

   const onCancel = () =&gt; 
    {
       setShow(false) 
    }

       return (
        &lt;ModalHead transparent={ true } visible = { show }             
         animationType=&#39;slide&#39; &gt;
           &lt;ModalArea&gt;
                    &lt;ModalBody&gt;
                        &lt;Voltar onPress = { (  =&gt; onCancel () } &gt;
                            &lt;IconVoltar width=&quot;30&quot; height=&quot;30&quot; fill=&quot;#000&quot; /&gt;
                        &lt;/Voltar&gt;
                   &lt;/ModalBody&gt;
           &lt;/ModalArea&gt;
       &lt;/ModalHead&gt;&quot;
}

答案1

得分: 2

看起来你可能需要将this绑定到打开模态框的处理程序。

这可以在构造函数中完成

constructor (props) {
  super(props);
  this.state = {
    listaFirebase: [],
    showModal: false,
    search: ''
  };

  this.openModal = this.openModal.bind(this);
}

或者可以使用箭头函数来完成

openModal = (value) => {
  this.setState({ showModal: value });
};

还有,似乎你没有正确地将setShow回调传递给模态框,看起来它应该openModal回调。

render () {
  return (
    <Container>
      <TouchableOpacity style={styles.addButton}
        activeOpacity={0.7}
        onPress={() => this.openModal(true)}
      >
        <Icon name="plus" size={20} color='#f2f2f2' />
      </TouchableOpacity>
      <TelaAdd               
        show={this.state.showModal}
        setShow={this.openModal} // <-- 将this.openModal作为setShow传递
      />
    </Container>
  );
}
英文:

Looks like you may need to bind this to the open modal handler.

This can be done in the constructor

constructor (props) {
  super(props)   ;
  this.state = {
    listaFirebase: [],
    showModal: false,
    search: &#39;&#39;              
  };

  this.openModal = this.openModal.bind(this);
}

Or can be done using an arrow function

openModal = (value) =&gt; {
  this.setState({ showModal: value });
};

It also appears you don't pass a setShow callback to the modal correctly, it looks like it should be the openModal callback.

render () {
  return (
    &lt;Container&gt;
      &lt;TouchableOpacity style={styles.addButton}
        activeOpacity={0.7}
        onPress={() =&gt; this.openModal(true)}
      &gt;
        &lt;Icon name=&quot;plus&quot; size={20} color=&#39;#f2f2f2&#39; /&gt;
      &lt;/TouchableOpacity&gt;
      &lt;TelaAdd               
        show={this.state.showModal}
        setShow={this.openModal} // &lt;-- pass this.openModal as setShow
      /&gt;
    &lt;/Container&gt;

huangapple
  • 本文由 发表于 2020年10月26日 05:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64529179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定