0

why i cant access to variable "rows" in method parse? If i put this.setState({rows: [3,2]}); outside of "parseString it works, but what to do to make it work inside this method?

 class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = this.getCleanState();
    this.parse = this.parse.bind(this);
 }


 getCleanState() {

    return {

        isLoading: true,
        columns:[
            { name: 'a:ClientID', title: 'ID kombinace'},
            { name: 'a:Name', title: 'Název kombinace'},
        ],
        defaultColumnWidths: [
            { columnName: 'a:ClientID', width: 160 },
            { columnName: 'a:Name', width: 200 },

          ],
        rows:[],
        selection: [],


    };
} 

 parse(XML){
        var parseString = require('react-native-xml2js').parseString;
        var xml = XML;
        parseString(xml, function (err, result) {
        console.dir(result);
        this.setState({rows: [3,2]});
        });
    }

1 Answer 1

1

because you use an anonymous function that is not binded.

Use arrow function for instance (or explicitly bind)

parse(XML){
    var parseString = require('react-native-xml2js').parseString;
    var xml = XML;
    parseString(xml, (err, result) => {
        console.dir(result);
        this.setState({rows: [3,2]});
    });
}
Sign up to request clarification or add additional context in comments.

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.