2

I want to pass an array to the cloneWithRows method. below is the code

export default class FirstReactNativeApp extends Component {

          constructor() {
            super();
            const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

          this.state = {
            modalVisible: false,
            todoText:'',
            todoListArray:['foo 1','foo 2'],
           dataSource: ds.cloneWithRows(this.state.todoListArray)

          };
        }

This is the Listview

<ListView
        dataSource={this.state.dataSource}
        renderRow={(rowData) => <Text>{JSON.stringify(rowData)</Text>}
/>

I'm getting the error undefined is not an object(evaluating '_this.state.todoListArray')

I'm not able to resolve this.Please guide me!

Thanks

3 Answers 3

3

Try this -

constructor() {
  super();
  this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

  this.state = {
    modalVisible: false,
    todoText:'',
    todoListArray:['foo 1','foo 2'],
  };
}

<ListView
        dataSource={this.ds.cloneWithRows(this.state.todoListArray)}
        renderRow={(rowData) => <Text>{JSON.stringify(rowData)</Text>}
/>
Sign up to request clarification or add additional context in comments.

2 Comments

While using this its still gave the error undefined is not an object(evaluating '_this.ds.cloneWithRows')
that's because you didn't change const ds to this.ds
1

You are accessing the state object before setting it. so it returns undefined. Try like this

export default class FirstReactNativeApp extends Component {

      constructor() {
        super();
        const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

      this.state = {
        modalVisible: false,
        todoText:'',
        todoListArray:['foo 1','foo 2'],
       dataSource: ds.cloneWithRows(['foo 1','foo 2'])

      };
  }

Then later when todoListArray content changes, you can set it to dataSource as

this.setState({
                dataSource: this.state.dataSource.cloneWithRows(this.state.todoListArray),
              });

Comments

0

The above answer given by vinayr gave me a clue.

      this.state = {
        modalVisible: false,
        todoText:'',
        todoListArray:['foo 1','foo 2'],
        dataSource: ds

      };

and then in the List View

<ListView
  dataSource= {this.state.dataSource.cloneWithRows(this.state.todoListArray)}
  renderRow={(rowData) => <Text>{rowData}</Text>} />

It works!

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.