ios - React-Native with Redux : API objects appear in my console. 日志但不是我的观点

标签 ios facebook react-native redux react-redux

我正在使用 reactreact-nativereact-native-router-flux 构建一个简单的运动应用程序react-redux,使用 redux 并尝试将热门新闻对象的 api 拉到我的 View 。我需要的对象显示在我的控制台日志上,但无法让它显示在我的 View 上。

我收到的错误是:

Cannot read property 'cloneWithRowsAndSections' of undefined

TypeError: Cannot read property 'cloneWithRowsAndSections' of undefined at NewsList.componentWillMount

我有什么遗漏的吗?

新闻列表:

export default class NewsList extends Component {
  constructor(props){
    super(props);
      const ds = new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 != row2,
        sectionHeaderHasChanged: (s1, s2) => s1 !== s2
       });
      this.state = {
        dataSource: ds.cloneWithRowsAndSections(props.ListData)
      }
  }

  componentWillReceiveProps(nextProps){
    const dataSource = this.state.dataSource.cloneWithRowsAndSections(nextProps.ListData);
    this.setState({dataSource});
  }


  componentWillMount(){
    // console.log("whats This: ",this.props.ListData);
    let data = this.props.ListData;
    this.setState({
      dataSource: this.ds.cloneWithRowsAndSections(data)
    });
  }

新闻_查看:

render(){
console.log('TopNews', this.state.TopNews);
if(this.state.TopNews.length == 0){
  return(
    <View style={styles.container}>

      <View style={styles.LiveStyle}>
        <Live style={styles.LiveStyle} />
      </View>

      <View style={styles.container}>
        <Text style={[styles.NotFollowTeamMsg, styles.centerTeamMsg]}>
          Your not connected to the api
        </Text>
      </View>
    </View>
  )
} else {
  return (
      <View style={styles.container}>
        <NewsList
          ListData={this.state.TopNews}


        />
      </View>
  )
}


 }

在构造函数中,我尝试将 const ds = ... 更改为 this.ds = ... 但这只会导致错误:

ds is not defined ReferenceError: ds is not defined

我尝试取出构造函数:

const ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 != row2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2
});

然后将其定位到 NewsList 类之外,然后我setState,如下所示:

this.setState({
  dataSource: ds.cloneWithRowsAndSections(data)
});

但这只会导致错误:

Cannot read property 'bind' of undefined TypeError: Cannot read property 'bind' of undefined

最佳答案

2 个选项:

1.在构造函数中创建 const ds = ...,如 this.ds = ...

2.将其从构造函数函数中取出:

const ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 != row2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2
});

并将其定位在 NewsList 类之外。

现在,当您setState时,请这样做:

this.setState({
  dataSource: ds.cloneWithRowsAndSections(data)
});

关于ios - React-Native with Redux : API objects appear in my console. 日志但不是我的观点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025388/

相关文章:

ios - 观察 NSUserDefaults 中个别设置的变化

ios - 如何检查 NSDictionary 键值是否为空?

ios - SwiftUI 组合 : TabView is not updating on selection when property stored in different viewmodel

facebook - 如何记录 Koala API 的请求和回复?

ruby-on-rails - NoMethodError,未定义方法 `bytesize' 用于带有 devise 和omniauth-facebook 的哈希

ios - 从 Firebase 缓存图片

Swift - Firebase 在 Firestore 中获取 Facebook 好友 UID

javascript - 以编程方式单击 react native 中的按钮

javascript - React Native SectionList scrollToLocation 不是函数

javascript - 使用 Cookie Jar 在 React Native Activity 和 Native Android Activity 之间共享 cookie