react-native的原生传参到rn
1.在安卓端的代码
package com.rndemo;
import android.os.Bundle;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "RNDemo";
}
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
@Override
protected Bundle getLaunchOptions() {
Bundle initialProperties = new Bundle();
// 传基本类型时直接获取出来就行
initialProperties.putString("images1", "http://p1.so.qhimgs1.com/bdr/_240_/t01a89e7f72db801e71.jpg");
initialProperties.putString("images2", "http://p2.so.qhimgs1.com/bdr/_240_/t01fdcfbcf2fa917c8e.jpg");
ArrayList<String> data = new ArrayList<>();
// 传集合用.map((url)=>console.log(url)) // 这样获取到
initialProperties.putStringArrayList("data",data);
return initialProperties;
}
};
}
}
- rn端代码
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state={
datas: []
}
}
renderImage() {
const images = [];
const datas = this.state.datas;
for (let i = 0; i < datas.length; i++) {
images.push(
<Image
key={i}
source={{uri: datas[i]}}
style={{width: 200, height: 200, backgroundColor: '#ff1900'}}
resizeMode="contain"
/>
)
}
return images;
}
// 即将渲染布局
componentWillMount() {
const image = this.props.images1;
ToastAndroid.show((typeof image),ToastAndroid.SHORT);
// const data = [];
// 将获取到的数据放在数组里
// image.map((url)=> data.push(url));
this.setState({
datas: [image],
})
}
render() {
return (
<View style={styles.container}>
{this.renderImage()}
</View>
);
}
}
安卓原生端传参到rn完。