2

I am using react-native-modalbox. What I want to do is open modal automatically when state is changed rather than using a button.

Below is my code it works fine with the button. But I want the modal to open when stateChange state is changed.

import React from 'react';
import Button from 'react-native-button';
import Modal from 'react-native-modalbox';
import {Text, StyleSheet, ScrollView, View, Dimensions, TextInput} from 'react-native';
var screen = Dimensions.get('window');

export default class TestScreen extends React.Component {

  constructor() {
    super();
    this.state = {
      isDisabled: false,
      number:''

//I want to open modal when this state is changed

      stateChange:''
    };
  }

  render() {

    return (
      <View style={styles.wrapper}>

// When I press this button it opens a modal  

        <Button onPress={() => this.refs.modal3.open()} style={styles.btn}>Position centered + backdrop + disable</Button>

        <Modal style={[styles.modal, styles.modal3]} position={"center"} ref={"modal3"} isDisabled={this.state.isDisabled}>
          <Text style={styles.text}>Modal centered</Text>
          <Button onPress={() => this.setState({isDisabled: !this.state.isDisabled})} style={styles.btn}>Disable ({this.state.isDisabled ? "true" : "false"})</Button>
        </Modal>

      </View>
    );
  }
}

Edited

I want to use react lifecycle method to listen to this update and automatically update it. So I am using componentDidUpdate. I want to update this.state.number when ever mobx store is updated. However, the code below wouldn't work.

componentDidUpdate(nextProps, nextState) {
    this.setState({number:store.requestObject.number},
    console.log(this.state.number), 'state changed')
  }

1 Answer 1

3

You can use isOpen property of the modal to open it based on the state. Take one property isOpen in your state and change it based on your preconditions, and isOpen will open the modal.

<Modal isOpen={this.state.isOpen}>content</Modal> 

Check this

Sign up to request clarification or add additional context in comments.

1 Comment

thanks very much for the answer, however, I have one issue, I am changing state from another screen so I need to listen for the change and update it but having some issue could you please see my edited question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.