rn 组件传参props
程序员文章站
2022-06-15 23:37:39
...
组件用法和react一致,参数设置和react一致
this.props.键名获取传入参数
代码示例:
//父组件
import React,{Component} from 'react'
import {View,Text,Alert,StyleSheet} from 'react-native'
export default class App extends Component{
render(){
return(
<>
<View>
<Ssr name='jeff'/>
</View>
</>
)
}
}
//子组件
class Ssr extends Component{
constructor(props){
super(props)
this.state={name:this.props.name}
}
render(){
return(
<>
<Text onPress={this.change}>{this.state.name}</Text>
<Text>{this.props.name}</Text>
</>
)
}
change(){
this.setData({
name:this.state.name
})
}
}