admin管理员组

文章数量:1336631

I need inside the file artistPage.js to refer to the TabNavigator in index.ios.js. In particular, I need to change the styles to hide the TabBar when the user is on the page artistPage.

How can I do that? Any ideas?

I tried to transfer styles in the props but there is the read-only mode(

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',
                ponent: 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({
        ponent: ArtistPage
      });
  }

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

module.exports = ImagesList;

artistPage.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;

Here is how to hide TabNavigator:

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

But I don't understand how to access it from artistPage.js

Thank you!

I need inside the file artistPage.js to refer to the TabNavigator in index.ios.js. In particular, I need to change the styles to hide the TabBar when the user is on the page artistPage.

How can I do that? Any ideas?

I tried to transfer styles in the props but there is the read-only mode(

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',
                ponent: 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({
        ponent: ArtistPage
      });
  }

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

module.exports = ImagesList;

artistPage.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;

Here is how to hide TabNavigator: https://github./exponentjs/react-native-tab-navigator

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

But I don't understand how to access it from artistPage.js

Thank you!

Share Improve this question edited Jul 5, 2016 at 12:49 Ivo Dimitrov asked Jul 5, 2016 at 12:42 Ivo DimitrovIvo Dimitrov 3171 gold badge4 silver badges12 bronze badges 1
  • Pass a function A to the child. The child calls the function A by calling this.props.functionA and passes parameters (data) to it. The parent deals with the passed data in the function A. – Kiwi Commented Jul 5, 2016 at 13:30
Add a ment  | 

1 Answer 1

Reset to default 6

Data flow in React is one way. What it means in practice is that, to change something that a certain ponent receives via props, it will need to call back into the parent ponent, via a function from props.

The React website has a nice intro to the concept.

In your particular case, you could have a tabBarVisible state in MyApp, and inside render, pute the style to apply to the tab bar.

MyApp also can have a method to change this state:

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

Now, in order to let ArtistPage toggle that, you can pass the hideTabBar function from MyApp to ArtistPage as a prop, and call it in ArtistPage in a lifecycle hook, like ponentDidMount.

本文标签: javascriptCommunicate Between Components in React Native (child gt parent)Stack Overflow