react-native的原生传参到rn

本文介绍了如何在React Native应用中从安卓原生代码传递参数,包括字符串和数组类型的参数。通过实例展示了如何设置和获取这些参数,并在React Native组件中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
            }
        };
    }
}
  1. 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完。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值