react-native-modal-selector - Modal don't close


#1

Hello,

I’m trying to use react-native-modal-selector (Modal Selector) but the modal don’t want to close on android when i try to update the state of my parent component.
Here is my code :

Children component :

<ModalSelector
            data={data}
            initValue="Référence dalle"
            cancelText={"Annuler"}
            animationType="none"
            onChange={item => {
              this.setState({ selected_item: item.key });
              this.props.setState("test", "test");
            }}
          />

Parent component :

  _setState(name, value) {
    console.log(name + " " + value);
    this.setState({ [name]: value });
  }
<InputSlabReference
              slab_ref={this.state.slab_ref}
              setState={this._setState}
/>

But if i do the following, it’s working :

<ModalSelector
            data={data}
            initValue="Référence dalle"
            cancelText={"Annuler"}
            animationType="none"
            onChange={item => {
              this.setState({ selected_item: item.key });
            }}
          />

In the source code of Modal Selector, i found this :

onChange = (item) => {
        if (Platform.OS === 'android' || !Modal.propTypes.onDismiss) {
            // RN >= 0.50 on iOS comes with the onDismiss prop for Modal which solves RN issue #10471
            this.props.onChange(item);
        }
        this.setState({ selected: this.props.labelExtractor(item), changedItem: item });
        this.close();
    }

Can somebody explain to me what I’m doing wrong?

Thanks :slight_smile:


#2

maybe there is an error that is being swallowed and preventing close from running

you need to bind _setState so this refers to the component instance. an arrow function does that automatically

  _setState = (name, value) => {
    console.log(name + " " + value);
    this.setState({ [name]: value });
  }

#3

Hello,

Thank you for your answer. I already did it but the problem is still here.
The parent state is updated but the modal il always open.


#4

Hi @lmichel - it’s pretty hard to help any more with just the code snippets you’ve provided. Could you perhaps post a snack that minimally reproduces this issue?


#5

Hello,

I founh why this is not working : Link


closed #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.