[英]props not getting passed to the another component in react native
When I pass a function as props to another component its not appearing in that component 当我将函数作为道具传递给另一个组件时,它不出现在该组件中
I tried to create a dummy component just below the parent component the props is getting passed. 我尝试在道具通过的父组件下方创建一个虚拟组件。 When i try to pass to a component which is imported it doesn't get passed.
当我尝试传递到导入的组件时,它不会传递。 I am using stack navigator to route, i don't know if its the reason that the props which i pass is not getting displayed.
我正在使用堆栈导航器进行路由,我不知道我通过的道具是否未得到显示的原因。
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
This is Practice.js file: 这是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
This is the console.log output I am getting: 这是我得到的console.log输出:
"Object {screenProps: undefined, navigation: Object}"
“对象{screenProps:未定义,导航:对象}”
The "toggle" which I have passed is not appearing
我已通过的“切换”未出现
You need to pass props in the constructor. 您需要在构造函数中传递道具。 For more details visit here https://reactjs.org/docs/react-component.html#constructor
有关更多详细信息,请访问此处https://reactjs.org/docs/react-component.html#constructor
The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs.
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
Error -1 : You need to pass navigation also to access that in different screen. 错误-1:您还需要通过导航才能访问其他屏幕。
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
navigation={this.props.navigation}
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
Basically you need to add this line instead of your old code. 基本上,您需要添加此行而不是旧代码。
<Practice
navigation={this.props.navigation}
toggle={this.toggle}/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.