REACT NATIVE-在关闭导入模式时更改屏幕的状态 [英] REACT NATIVE - Change state of screen when close import modal

查看:91
本文介绍了REACT NATIVE-在关闭导入模式时更改屏幕的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我关闭模式时,我需要检测它是否已关闭以更改父页面的状态.当我更改状态的任何属性(模态)时,都无法更改它.

When I close the modal, I need to detect that it has been closed to change the state of the parent page. Not being able to change it when I change any property of the state, the modal.

ExpertFeedback.js

ExpertFeedback.js

import ModalExpertFeedback from './ModalExpertFeedback';

export default class ExpertFeedback extends Component {

   constructor(props) {
      super(props);      

      this.state = {
        modalVisible: false,
        projects: [{name:'project0', name:'project1'}],
        feedback: {title: '', content: '', project_id: ''}
      };
    }

    proveProjectIsntNull() {
      if (this.state.feedback.project_id != null){
        this.setModalVisible(true);
      } else {
        this.setModalVisible(false);
        Alert.alert('Select a project please');
      }
    }

    setModalVisible(visible) {
      this.setState({modalVisible: visible});
    }

    render() {
        return (
          <View>
            <View>
               <TextInput
                    placeholder="Write title"
                    onChangeText={(feedback_title) => this.setState( prevState => ({
                      feedback: {
                          ...prevState.feedback,
                          title: feedback_title
                      }}))
                    }                        
                    value={this.state.feedback.title}
               />
               <Picker
                    selectedValue={this.state.feedback.project_id}
                    onValueChange={(itemValue, itemIndex) => this.setState( prevState => ({
                      feedback: {
                          ...prevState.feedback,
                          project_id: itemValue
                      }}))
                    }>
                    <Picker.Item label="Select a project" value={null} />
                    {typeof this.state.projects === 'object' && this.state.projects.length && (this.state.projects.map((project, index) => {
                        return (<Picker.Item label={project.name} value={project.id} />)
                      }))}
                  </Picker>
            </View>
            <ModalExpertFeedback visible={this.state.modalVisible} navigation={this.props.navigation} feedback={this.state.feedback} />
                  <TouchableOpacity
                        onPress={() => {
                          this.proveProjectIsntNull();
                        }}>
                        <View>
                          <Text>SEND NOW</Text>
                        </View>
                   </TouchableOpacity>
           </View>
    )

    }
}

ModalExpertFeedback.js

ModalExpertFeedback.js

export default class ExpertFeedback extends Component {

    feedback = {
      title: "",
      content: "",
      project_id: "",
    };

    state = {
      modalVisible: false
    };

    setModalVisible(visible) {
      this.setState({modalVisible: visible});
    }

    componentWillReceiveProps(props) {
      this.setState({ modalVisible: props.visible});
      this.setState({ feedback: props.feedback });
    }

    render() {
        return (
              <View>
                <Modal
                  animationType="slide"
                  transparent={true}
                  visible={this.state.modalVisible}
                  onRequestClose={() => { console.log('close') }} >

                  <View>   
                    <TouchableOpacity
                      onPress={() => {
                        this.setModalVisible(false);
                      }}
                      >
                      <View>
                        <Text>Close</Text>
                      </View>
                    </TouchableOpacity>
                  </View>
                </Modal>
              </View>
        )
    }
}

当我在ExpertFeedback中使用TextInput更改feedback.title时,模态会打开

When I change feedback.title with TextInput in ExpertFeedback, the Modal opens

推荐答案

如果您要连接"父级和子级,则需要传递一个处理程序,本质上是将其作为道具的功能从父级传递到您的父级孩子.

If you want to 'connect' the Parent and the Child, you'll need to pass a handler, essentially a function as a prop from your Parent to your child.

以下示例:

ExpertFeedback.js

ExpertFeedback.js

    parentHandler(result){
        //do your update here
        this.setState({result});
    }

 <ModalExpertFeedback 
    visible={this.state.modalVisible} 
    navigation={this.props.navigation} 
    feedback={this.state.feedback} 
    handler={this.parentHandler.bind(this)} />

ModalExpertFeedback.js

ModalExpertFeedback.js

      <Modal
          animationType="slide"
          transparent={true}
          visible={this.state.modalVisible}
          onRequestClose={() => { this.props.handler(someValue) }} >

这篇关于REACT NATIVE-在关闭导入模式时更改屏幕的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆