reactjs - props 没有传递给 react native 中的另一个组件

当我将函数作为 props 传递给另一个组件时,它不会出现在该组件中

我试图在传递 Prop 的父组件下方创建一个虚拟组件。当我尝试传递给导入的组件时,它没有被传递。我正在使用堆栈导航器进行路由,我不知道是否是我传递的 Prop 没有显示的原因。

import React from 'react'
import {View, Text, TouchableOpacity, Image, StyleSheet, Dimensions, TouchableWithoutFeedback} from 'react-native'

import DrawNavContents from './DrawNavContents'
import Practice from './Practice'

let width = Dimensions.get('window').width
let height = Dimensions.get('window').height

class DrawNav extends React.Component { 
    constructor(){
        super()
        this.state = {
            toggleDrawer: false,
            imgPos: 30
      }
  }

toggle = () => {
    this.setState(() => ({
        toggleDrawer: !this.state.toggleDrawer,
        imgPos: this.state.imgPos == 30 ? 300 : 30
    }))
}

render () {
    return (
        <View>
            <TouchableOpacity onPress={this.toggle}>
                <Image
                    source={require('../../images/drawer-150x150.png')}
                    style={{ width: 25, height: 25, marginLeft: this.state.imgPos }}
                    onPress= {this.toggle}
                />
            </TouchableOpacity>

            { 
                this.state.toggleDrawer && (
                    <View style={styles.menu}>
                        <DrawNavContents />
                    </View>
                )
            }
            <Practice toggle={this.toggle}/>
        </View>
        )
    }
}

const styles = StyleSheet.create({
    menu: {
        flex: 1,
        backgroundColor: 'yellow',
        position : 'absolute',
        left: 0,
        top: 0,
        width : width * 0.8, 
        height : height,
        paddingTop : 10,
        paddingLeft : 10,
        paddingRight : 10,
        paddingBottom : 10
    },
})

export default DrawNav

这是 Practice.js 文件:

import React from 'react'
import {View, Text} from 'react-native'

class Practice extends React.Component{
    render(){
        console.log("this is props", this.props)
        return(
            <View>
                <Text>hello</Text>
            </View>
        )
    }
}

export default Practice

这是我得到的 console.log 输出:

"Object {screenProps: undefined, navigation: Object}"

The "toggle" which I have passed is not appearing

最佳答案

您需要在构造函数中传递 Prop 。有关详细信息,请访问此处 https://reactjs.org/docs/react-component.html#constructor

React 组件的构造函数在挂载之前被调用。在为 React.Component 子类实现构造函数时,您应该在任何其他语句之前调用 super(props) 。否则,this.props 将在构造函数中未定义,这会导致错误。

import React from 'react'
import {View, Text, TouchableOpacity, Image, StyleSheet, Dimensions, TouchableWithoutFeedback} from 'react-native'

import DrawNavContents from './DrawNavContents'
import Practice from './Practice'

let width = Dimensions.get('window').width
let height = Dimensions.get('window').height

class DrawNav extends React.Component { 
    constructor(props){
        super(props)
        this.state = {
            toggleDrawer: false,
            imgPos: 30
      }
  }

toggle = () => (
this.setState((prevState) => ({
        toggleDrawer: !prevState.toggleDrawer,
        imgPos: prevState.imgPos == 30 ? 300 : 30
    }))
)

render () {
    return (
        <View>
            <TouchableOpacity onPress={this.toggle}>
                <Image
                    source={require('../../images/drawer-150x150.png')}
                    style={{ width: 25, height: 25, marginLeft: this.state.imgPos }}
                    onPress= {this.toggle}
                />
            </TouchableOpacity>

            { 
                this.state.toggleDrawer && (
                    <View style={styles.menu}>
                        <DrawNavContents />
                    </View>
                )
            }
            <Practice toggle={this.toggle}/>
        </View>
        )
    }
}

const styles = StyleSheet.create({
    menu: {
        flex: 1,
        backgroundColor: 'yellow',
        position : 'absolute',
        left: 0,
        top: 0,
        width : width * 0.8, 
        height : height,
        paddingTop : 10,
        paddingLeft : 10,
        paddingRight : 10,
        paddingBottom : 10
    },
})

export default DrawNav

https://stackoverflow.com/questions/57950921/

相关文章:

java - 从 jlink 图像创建所有包含 windows 可执行文件

keras - 在不运行模型的情况下计算 Keras 中的损失

c++ - 使用 ioctl 在 C++ 中以编程方式添加路由

javascript - 要求 Prop 不起作用。 "Invalid call at.."

python - 使用 image_slicer.py 将切片加入完整图像

reactjs - 尝试在 react-testing-library 中呈现组件时出错 - 得到一

c# - Bot 在 MS TEAMS channel 中尝试发送自适应卡时抛出 "Operatio

npm - 无法在 Nexus Repository Manager 2.10 上发布库

azure-functions - Azure 函数无法在 Azure 门户中下载 PowerShe

sass - 找不到要导入的样式表