通过使用 {pros.name} 来获得从 index.js 中传过来的值,
- function HelloComponent(pros){
- return ( <Text style={{fontSize:20,backgroundColor:'red'}}>Hello 函数式 {pros.name}</Text> );
- }
- module.exports=HelloComponent; // 导出
在 index.js 中
- import HelloComponent from './HelloComponent' // 加入此行
- export default class simple extends Component {
- render() {
- return (
- <View style={styles.container}>
- <HelloComponent name = "小明"/> // 使用 HelloComponent 组件
- </View> );
- } }
第一种和第二种方式也可以通过 {this.pros.name} 来获得 index.js 中传过来的值
来源: https://juejin.im/entry/5ae923aef265da0b71561307