最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Callback function within map - Stack Overflow

programmeradmin1浏览0评论

I'm new to Reactjs and making a directory browsing application with Cordova and Reactjs.

Currently I get the error this.props.onClick is not a function, I figured out that this.props.onClick is undefined. Check my code below:

var RepositoryList = React.createClass({
    //updating the list from a child folder entry
    updateListFromChild:function(childFolder){ 
        console.log("in parent update");
    },    
    render:function(){
        console.log(this.updateListFromChild); //function()
        var itemsInRepo = this.props.items.map(function(entry){
            console.log("in map: " + this.updateListFromChild); //undefined
            return (
                <RepositoryEntry repositoryItem={entry} 
                                 folderOnClick={this.updateListFromChild} />
            );
        });
        return (
            <ul className="table-view">
                {itemsInRepo}
            </ul>
        );
    }
});

The property folderOnClick is undefined. But I don't know how to solve this problem. Can someone point this out?

I'm new to Reactjs and making a directory browsing application with Cordova and Reactjs.

Currently I get the error this.props.onClick is not a function, I figured out that this.props.onClick is undefined. Check my code below:

var RepositoryList = React.createClass({
    //updating the list from a child folder entry
    updateListFromChild:function(childFolder){ 
        console.log("in parent update");
    },    
    render:function(){
        console.log(this.updateListFromChild); //function()
        var itemsInRepo = this.props.items.map(function(entry){
            console.log("in map: " + this.updateListFromChild); //undefined
            return (
                <RepositoryEntry repositoryItem={entry} 
                                 folderOnClick={this.updateListFromChild} />
            );
        });
        return (
            <ul className="table-view">
                {itemsInRepo}
            </ul>
        );
    }
});

The property folderOnClick is undefined. But I don't know how to solve this problem. Can someone point this out?

Share Improve this question edited May 5, 2015 at 11:24 php-dev 7,1764 gold badges26 silver badges38 bronze badges asked May 5, 2015 at 9:51 user2810895user2810895 1,3744 gold badges19 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

In general - this is the "dynamic this" issue in JavaScript.

However, since you're using ReactJS with JSX, your code has support for ES6 arrow functions in the transform anyway:

var itemsInRepo = this.props.items.map(entry => (                
   <RepositoryEntry repositoryItem={entry} folderOnClick={this.updateListFromChild} />
));

You can see the syntax on MDN for more examples.

We can proceed with this way also. Here is the code snippet:

var itemsInRepo = this.props.items.map( 
        function(entry){                
            return(
             <RepositoryEntry 
               repositoryItem={entry} 
               folderOnClick={this.updateListFromChild} 
        />);
发布评论

评论列表(0)

  1. 暂无评论