Skip to content

NavigationExperimental: Unnecessary rendering in NavigationView. #6579

@5amfung

Description

@5amfung

While I was testing out and tried to understand how NavigationCompositionExample works, I found that renderScene() was called 3 times (exactly the number of tabs) for a single view. If you have N tabs, you will see N x N times of the debug log. I don't quite understand it. It doesn't seem it's necessary.

class ExampleMainView extends React.Component {
  render() {
    return (
      <NavigationView
        navigationState={this.props.navigationState}
        style={styles.tabsContent}
        renderScene={(tabState, index) => {
          console.log('index:', index);  // <<<<< This is printed 3x.
          return (
            <ExampleTabScreen
              key={tabState.key}
              navigationState={tabState}
              onNavigate={this._handleNavigation.bind(this, tabState.key)}
            />
          );
        }}
      />
    );
  }
  //...
}

The output:

'index:', 0
'index:', 1
'index:', 2

I think, the culprit is render() in NavigationView. It runs map() over children. But why?

var NavigationView = React.createClass({
  propTypes: {
    // todo, figure out a propType for getK
    navigationState: React.PropTypes.object.isRequired,
    renderScene: React.PropTypes.func.isRequired,
  },
  render: function() {
    return (
      <View
        style={this.props.style}>
        {this.props.navigationState.children.map(this._renderScene)}
      </View>
    );
  },
  _renderScene: function(route, index) {
    var isSelected = index === this.props.navigationState.index;
    return (
      <View
        key={route.key}
        pointerEvents={isSelected ? 'auto' : 'none'}
        style={[
          styles.navView,
          {opacity: isSelected ? 1 : 0},
        ]}>
        {this.props.renderScene(route, index)}
      </View>
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions