javascript - 引用错误: style is not defined

标签 javascript reactjs react-native

为什么我收到错误:“ReferenceError:样式未定义”?

我尝试了 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
    }


  });

最佳答案

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

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

替换:

import {MyModal} from './MyModal';

与:

import MyModal from './MyModal';

关于javascript - 引用错误: style is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58545320/

相关文章:

javascript - 元素在搜索后渲染 ReactJS

android - 类型错误 : null is not an object (evaluating 'RNGestureHandlerModule.default.Direction' )

Javascript:带有变量的正则表达式,用于匹配包含 HTML 代码的字符串的一部分

javascript - 如何在大型函数上使用 .load(function() {

reactjs - React - 新的 Context API 不适用于 Class.contextType,但适用于 Context.Consumer

firefox - 在 Safari 和 Firefox 中的 React 中重新渲染时插入符号位置恢复到 contenteditable 跨度的开始

javascript - 如何解决文本字符串必须在嵌套 map 中的 <Text> 内呈现?

javascript - 为什么我无法在 React 组件中访问我的 Mongo 集合?

javascript - 在两个可拖动的 Flatlist-React Native 之间拖放

react-native - 如何禁用可触摸组件中子元素的触摸