【问题标题】:ReferenceError: style is not definedReferenceError:样式未定义
【发布时间】:2019-10-25 00:20:33
【问题描述】:

为什么会出现错误:'ReferenceError: style is not defined'?

我尝试过import { MyModal } from './src/components/MyModal',但随后出现错误: 不变违规:不变违规:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

App.js

import MyModal from './src/components/MyModal'

class Home extends Component {
   constructor(props) {
  super(props)

  this.state = {
      isModalVisible: false
   }
  }

   doSomething(){
     //
   }

   render(){
        return (
          <View style={styles.contentContainer}
            <Modal transparent={true}
                        visible={this.state.isModalVisible} 
                        onRequestClose={() => this.doSomething()}
                        animationType='fade'>
                           <MyModal changeModalVisibility = {this.doSomething} />
                  </Modal>
          </View>

    )
  }
}

   const styles = StyleSheet.create({
    contentContainer: {
      flex: 1,   
      alignItems: 'center',
      justifyContent: 'center'
    },
   });

MyModal.js

export default class MyModal extends Component {

    constructor(props) {
        super(props)

        this.state = {
          width: Dimensions.get('window').width
        };

        Dimensions.addEventListener('change', (e) => {
          this.setState(e.window);
        })
    }


   close = () => {
     this.props.doSomething();
   }



    render() {
       return(

             <TouchableOpacity activeOpacity={1} disabled={true} style={styles.contentContainer}>
                 <View style={[styles.modal, {width: this.state.width}]}>
                     <View style={styles.textView}>
                         <Text style={[styles.text, {fontSize: 16}]}> Modal Header </Text>
                     </View>
                     <View style={style.buttonsView}>
                         <TouchableHighlight onPress={() => this.close()} style={styles.touchable}>
                             <Text style={[styles.text, {color: 'orange'}]}> </Text>
                         </TouchableHighlight>
                     </View>
                 </View>
             </TouchableOpacity>
       )
    }
}




const styles = StyleSheet.create({
    contentContainer: {
      flex: 1,   
      alignItems: 'center',
      justifyContent: 'center'
    },
    modal: {
      height: 150,
      paddingTop: 10,
      alignSelf: 'center',
      alignItems: 'center',
      textAlign: 'center',
      borderRadius: 10,
      backgroundColor: 'white'
    },
    text: {
      margin: 5,
      fontSize: 16,
      fontWeight: 'bold'
    },
    touchable: {
      flex: 1,
      backgroundColor: 'white',
      paddingVertical: 10,
      alignSelf: 'stretch',
      alignItems: 'center',
      borderRadius: 10
    }


  });

【问题讨论】:

    标签: javascript reactjs react-native


    【解决方案1】:

    对于样式错误,请将MyModal 中的style.buttonsView 替换为styles.buttonsView

    对于Invariant Violation 错误,您将MyModal 作为默认导出导出,但尝试将其导入为命名导出。

    替换:

    import {MyModal} from './MyModal';
    

    与:

    import MyModal from './MyModal';
    

    【讨论】:

    • 你做到了!!我也没有定义 buttonView 或 textView 样式。先生有一双非常锐利的眼睛!
    • 如果您认为@ehynds 的答案解决了您的问题,请将其标记为正确答案
    猜你喜欢
    • 2012-08-24
    • 2015-10-04
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多