【问题标题】:React Native: Integration With Existing AppsReact Native:与现有应用程序集成
【发布时间】:2017-02-28 18:23:43
【问题描述】:

我是 react 新手,我遵循了 React Native Docs 中关于集成现有应用程序的教程。

private ReactRootView mReactRootView;
.......

Bundle launchOptions = new Bundle();
launchOptions.putBoolean("test", true);
//mReactRootView.startReactApplication(mReactInstanceManager, "ThirdAwesomeComponent", launchOptions);
mReactRootView.startReactApplication(mReactInstanceManager, "ThirdAwesomeComponent", null); // Actual example

有没有办法在 index.android.js 处读取 HelloWorld 组件中的 launchOptions?

另外,我有两个活动,我需要调用 react native 守护进程并希望呈现服务器返回的两个不同布局。

由于目前我只有一个,我该怎么做:

AppRegistry.registerComponent('HelloWorld', () => HelloWorld);

【问题讨论】:

    标签: android facebook react-native


    【解决方案1】:

    最好的方法是做类似的事情,

    从索引页面重定向到 App.js 使用

    AppRegistry.registerComponent("App",()=>App)

    这将重定向到应用程序

    然后根据服务器输出渲染两个不同的场景。您可以创建一个状态变量并将其初始化为默认状态。

    在组件的渲染功能中,您可以检查状态值并根据需要分配布局。

    使用类似的东西

      export default Class App extends Component{
    constructor(props){
    super(props)
    this.state{
        data1:false,
        data2:true,
        loaded:false,
    }
    }
    
    
    //do all the fetching data to server here
    
    
    componentWillMount(){
    //after fetching the data to server
    
        change the state as
    
        this.setState({
            data1:true,
            data2:false,
            loaded:true
        })
    }
    
    
    render({
        if(this.state.loaded && this.state.data1){
            return(
                //layout which you want to return
            )
        }else if( this.state.loaded && this.state.data2){
            return(
                //second layout code
            )
        }else{
             return(
                    //can create a loading spinner here
                  <Text>Loading.....</Text>
             )
         }
      })
    }
    

    希望对你有帮助

    干杯

    【讨论】:

    • 在调用 mReactRootView.startReactApplication(mReactInstanceManager, "ThirdAwesomeComponent", launchOptions);例如,我想将端口更改为 8899 并在其上托管服务器。
    • 你说的是网络服务器还是本地节点服务器?我猜可以使用stackoverflow.com/questions/34431052/… 更改本地服务器。我对您更改服务器的事情一无所知。提供有关您正在尝试实施的内容的更多信息是否方便?
    【解决方案2】:

    您的启动选项将作为道具传递给组件的构造函数。 只需实现构造函数

    constructor(props){
      super(props)
      // do stuff to pops dictionary
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 2017-06-26
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2017-05-02
      相关资源
      最近更新 更多