【问题标题】:Communicate Between Components in React Native (child -> parent)在 React Native 中的组件之间进行通信(子 -> 父)
【发布时间】:2016-11-07 07:29:02
【问题描述】:

我需要在 artistPage.js 文件中引用 index.ios.js 中的 TabNavigator。特别是,当用户在页面 artistPage 上时,我需要更改样式以隐藏 TabBar。

我该怎么做?有什么想法吗?

我尝试在props中传输样式,但是有只读模式(

index.ios.js

'use strict'

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  View,
  Image,
  Text,
  NavigatorIOS,
  TouchableHighlight,
  NavigationBar,
} from 'react-native';
import config from './config';

import ImagesList from './app/imagesList';
import TabNavigator from 'react-native-tab-navigator';
import Badge from './node_modules/react-native-tab-navigator/Badge'

class MyApp extends Component {
  constructor(props) {
    super(props);

    this.state = {
      selectedTab: 'images',
      showTabBar: true
    };
  }

  render() {
    let tabBarStyle = {};
    let sceneStyle = {};
    if (this.state.showTabBar) {
      tabBarStyle = styles.tabBar;
      sceneStyle.paddingBottom = 54;
    } else {
      tabBarStyle.height = 0;
      tabBarStyle.overflow = 'hidden';
      sceneStyle.paddingBottom = 0;
    }
    return (
      <View style={styles.container}>
        <TabNavigator 
          tabBarStyle={ tabBarStyle } 
          sceneStyle={sceneStyle} 
          >
          <TabNavigator.Item
            titleStyle={styles.title}
            selectedTitleStyle={styles.title_select}
            selected={this.state.selectedTab === 'images'}
            title="TATTOOS"
            renderIcon={() => <Image source={require('./images/tabbar/tattoos_icon.png')} />}
            renderSelectedIcon={() => <Image source={require('./images/tabbar/tattoos_icon_selected.png')} />}
            onPress={() => this.setState({ selectedTab: 'images' })}>
            <NavigatorIOS
              style={styles.container}
              initialRoute={{
                title: 'MyApp',
                component: ImagesList,
                passProps: { showTabBar: true},
              }}
              navigationBarHidden={true}/>
          </TabNavigator.Item>

        </TabNavigator>
      </View>
    );

  }
}

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

imageList.js

'use strict'

import React, { Component } from 'react';
import {
  StyleSheet,
  ListView,
  View,
  Text,
  Image,
  Dimensions,
  ActivityIndicator,
  TouchableHighlight,
  RefreshControl
} from 'react-native';

import ArtistPage from './imageCard';

class ImagesList extends Component {
  constructor(props) {
    super(props);

    this.state = {
    };
  }

  _artistPage() {
    this.props.navigator.push({
        component: ArtistPage
      });
  }

  render() {
      return (
        <View style={styles.container}>
          <TouchableHighlight
            onPress={this._artistPage()}
            >
            <Text>Got to Next Page</Text>
          </TouchableHighlight>
        </View>
      );
    }
  }
}

module.exports = ImagesList;

艺术家页面.js

'use strict'

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  ListView,
  View,
  TouchableHighlight,
  Image,
} from 'react-native';

class ArtistPage extends Component {
  constructor(props) {
    super(props);

    this.state = {

    };
  }

  _backTo() {
    this.props.navigator.pop();
  }

  render() {
    return (
      <View>
        <TouchableHighlight style={{marginTop: 100, marginLeft: 50}} onPress={() => this._backTo()} >
          <Text>Back {this.props.showTabBar.toString()}</Text>
        </TouchableHighlight>
      </View>
    );
  }
}


module.exports = ArtistPage;

这里是隐藏 TabNavigator 的方法:https://github.com/exponentjs/react-native-tab-navigator

let tabBarHeight = 0;
<TabNavigator
  tabBarStyle={{ height: tabBarHeight, overflow: 'hidden' }}
  sceneStyle={{ paddingBottom: tabBarHeight }}
/>

但我不明白如何从 artistPage.js

访问它

谢谢!

【问题讨论】:

  • 将函数 A 传递给子级。子进程通过调用 this.props.functionA 调用函数 A 并将参数(数据)传递给它。 parent 在函数 A 中处理传递的数据。

标签: javascript ios node.js reactjs react-native


【解决方案1】:

React 中的数据流是一种方式。这实际上意味着,要更改某个组件通过 props 接收的内容,它需要通过 props 中的函数回调父组件。

React 网站有 a nice intro 的概念。

在您的特定情况下,您可以在MyApp 中拥有tabBarVisible 状态,并在渲染内部计算要应用于标签栏的样式。

MyApp也可以有一个方法来改变这个状态:

hideTabBar() {
  this.setState({ tabBarVisible: true });
}

现在,为了让ArtistPage 切换,您可以将hideTabBar 函数从MyApp 传递给ArtistPage 作为道具,并在lifecycle hook 中调用ArtistPage,例如@ 987654332@.

【讨论】:

  • React 更容易推理,但是放入一个组件,该组件可以通过引用其父容器的位置来计算其滚动到顶部,这似乎令人困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 2016-07-19
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多