简体   繁体   English

React Native:renderRow未执行

[英]React Native: renderRow not executed

In the following code, when setState is called from campaignsUpdated , render gets logged to the console, but not renderRow : 在以下代码中,当从campaignsUpdated调用setState时, render会记录到控制台,但不会记录到renderRow

var React = require('react-native'),

Bus = require('../Bus'),
styles = require('../Styles'),
CampaignsStore = require('../stores/Campaigns'),
CampaignItem = require('./CampaignItem'),

{
  Component,
  Text,
  TextInput,
  ListView,
  View,
  NavigatorIOS,
  ActivityIndicatorIOS
} = React


class CampaignList extends Component {
    constructor(props) {
      super(props)

      this.state = {
        dataSource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
      }
    }

    componentDidMount() {
      this.addListeners()
      Bus.emit('campaigns:search', '')
    }

    componentWillUnmount() {
      this.removeListeners()
    }

    render() {
      console.log('render')
      return (
        <View style={styles.container}>
          <TextInput
            style={styles.searchInput}
            placeholder='Campaign Name'
            value={this.state.campaignName}
            onChange={this.campaignSearchChanged.bind(this)}/>
          <ListView
            dataSource = {this.state.dataSource}
            renderRow = {this.renderRow.bind(this)}/>
        </View>
      )
    }

    renderRow(campaign) {
      console.log('renderRow')
      return <CampaignItem campaign={campaign}/>
    }

    addListeners() {
      Bus.on({
        'campaigns:updated': this.campaignsUpdated.bind(this)
      })
    }

    removeListeners() {
      Bus.off({
        'campaigns:updated': this.campaignsUpdated.bind(this)
      })
    }

    campaignsUpdated(event) {
      var campaigns = event.data
      this.setState({
        dataSource: this.state.dataSource.cloneWithRows(campaigns)
      })
    }

    campaignSearchChanged(event) {
      var campaignName = event.nativeEvent.text
      Bus.emit('campaigns:search', campaignName)
    }
}

module.exports = CampaignList

What am I doing wrong here? 我在这做错了什么?

You are passing ListView a function renderRow that returns a component. 您正在向ListView传递一个返回组件的函数renderRow You would have to call that function within ListView once it is passed, presumably during a map over campaigns . 一旦传递了ListView ,就必须在ListView调用该函数,大概是在campaigns的地图上。

By the looks of it the most likely case is that you have a classic React mutability issue here. 从它的外观来看,最可能的情况是你在这里有一个经典的React可变性问题。

Ie I suspect your 'campaignsUpdated' method is called with either the same Array instance it received last time, or the elements within the list are the same instances. 即我怀疑你的'campaignsUpdated'方法是使用上次收到的相同Array实例调用的,或者列表中的元素是相同的实例。

Try using: 尝试使用:

campaignsUpdated(event) {
  var campaigns = event.data.slice(); // <-- clone the array
  this.setState({
    dataSource: this.state.dataSource.cloneWithRows(campaigns)
  })
}

If that doesn't work, then you either make the part that manages your list of compaigns create new copies when changes are made (eg const clone = {...campaign, title:"A new Title"} ) or update your rowHasChanged method to see if the title (or whatever data you need) has actually changed. 如果这不起作用,那么您可以使管理您的比较列表的部分在进行更改时创建新副本(例如const clone = {...campaign, title:"A new Title"} )或更新您的rowHasChanged查看标题(或您需要的任何数据)是否实际发生变化的方法。

Here are two really good videos about immutability in JavaScript here: https://egghead.io/lessons/javascript-redux-avoiding-array-mutations-with-concat-slice-and-spread https://egghead.io/lessons/javascript-redux-avoiding-object-mutations-with-object-assign-and-spread 这里有两个关于JavaScript不变性的非常好的视频: https//egghead.io/lessons/javascript-redux-avoiding-array-mutations-with-concat-slice-and-spread https://egghead.io/lessons / JavaScript的终极版回避对象突变与-对象指派和扩频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在React Native中的renderRow()函数内使用javascript .replace()方法 - Unable to use javascript .replace() method inside the renderRow() function in React Native react-native renderRow with button:将 rowData 推送到按钮功能 - react-native renderRow with button: push rowData to button-function React Native:将数据源从_renderRow中的道具传递到方法中 - React Native: Passing DataSource into Method from Props in _renderRow React-Native:无法访问状态值或从renderRow声明的任何方法 - React-Native: Cannot access state values or any methods declared from renderRow 执行渲染方法之前数据不可用 - React Native - Data unavailable before render method is executed - React Native react-native axios解析回调函数未执行 - react-native axios resolve callback function not executed React Native用什么让JavaScript在iOS和Android上原生执行? - What does React Native use to allow JavaScript to be executed on iOS and Android natively? 在 React native 中执行 function 时,如何根据条件更改 useState 值? - How can I change the useState value according to the condition when the function is executed in React native? React Native 应用程序的 expo-sqlite 回调直到应用程序刷新才执行 - React Native app's expo-sqlite callback not executed until app refresh 开关值未在renderRow中更新 - Switch value not updating in renderRow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM