Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does there have a more beautiful way to configure node on the List? #24

Open
h3l opened this issue Nov 30, 2015 · 0 comments
Open

Does there have a more beautiful way to configure node on the List? #24

h3l opened this issue Nov 30, 2015 · 0 comments

Comments

@h3l
Copy link

h3l commented Nov 30, 2015

Question

The demo use componentDidMount to configure the node like below:

componentDidMount: function() {
    // ScrollView
    RCTRefreshControl.configure({
      node: this.refs[SCROLLVIEW],
      tintColor: '#05A5D1',
      activityIndicatorViewColor: '#05A5D1'
    }, () => {
      this.setTimeout(() => {
        RCTRefreshControl.endRefreshing(this.refs[SCROLLVIEW]);
      }, 2000);
    });

    // ListView
    RCTRefreshControl.configure({
      node: this.refs[LISTVIEW]
    }, () => {
      this.setTimeout(() => {
        RCTRefreshControl.endRefreshing(this.refs[LISTVIEW]);
      }, 2000);
    });
  },

but in Performance, it recommend to use a LoadingPage before navigator transitions.so my render function code is like below:

  render: function(){
    if(this.state.isLoading){
      return <LoadingPage />
    }
    return(
        <ListView
          ref="ListView"
          dataSource={this.state.dataSource}
          renderRow={this._renderRow}/>
    )
  },

so when the componentDidMount be triggered, there is no ListView but a LoadingPage.

Solution(not beautiful)

the Pseudo code is below

var ExamplePage = React.createClass({
  mixins: [utilsMixin, TimerMixin],
  getInitialState: function(){
    var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    return {
      dataSource: ds.cloneWithRows([]),
      isLoading: true,
      isRefreshControlLoaded: false
    }
  },
  _fetch: function(){
    fetch(url)
    .then((res) => {
      return res.json();
    })
    .then((resJson) => {
      if(this.state.isRefreshControlLoaded === true){
        RCTRefreshControl.endRefreshing(this.refs["ListView"]);
      }
      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(resJson),
        isLoading: false,
      });
    })
    .catch(e => {
      console.log(e);
    })
    .done();
  },
  componentDidMount: function(){
    InteractionManager.runAfterInteractions(() => {
      this._fetch();
    });
  },
  componentDidUpdate: function(prevProps, prevState){
    if((this.state.isRefreshControlLoaded === false) && (this.state.isLoading === false)){
      // ListView 
      this.setState({isRefreshControlLoaded: true});
      RCTRefreshControl.configure({
        node: this.refs["ListView"],
      }, () => {
        this._fetchData()
      });
    }

  },
  _renderRow: function(rowData){
    return (<Text>rowData</Text>);
  },

  render: function(){
    if(this.state.isLoading){
      return <LoadingPage/>
    }
    return(
        <ListView
          ref="ListView"
          dataSource={this.state.dataSource}
          renderRow={this._renderRow}/>
        );
  },
});

Does there have a more beautiful way to solve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant