将参数传递给根组件

Pass params to root component

你能在 React Native 中将参数(如初始 props)传递给根组件吗?

例如

import { AppRegistry } from 'react-native'
import { Volcalc } from './app'

AppRegistry.registerComponent('volcalc_m', () => Volcalc({height:10,width:10}))

我收到错误:

Unable to execute JS call: fbBatchedBridge is undefined

由于 Volcalc 是您的根组件,只需在该方法的构造函数中指定组件属性:

class Volcalc extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            height: 10,
            width: 10,
            ...
        }

        ...
    }

    ...

如果您想要根据平台(iOS 和 Android)使用不同的高度,则在 index.android.jsindex.ios.js 中创建一个根组件来呈现 Volcalc 中的组件:<Volcalc height={10} width={10} />.