简体   繁体   English

reactjs-函数在map函数内部不起作用

[英]reactjs - Function not working inside a map function

I have a component to display list of tags , and the user can select tags to follow them. 我有一个显示标签列表的组件,用户可以选择标签来跟随它们。 The tags are displaying fine. 标签显示正常。 I would like to get the selected tag and store it inside a new array tagsSelectedList . 我想获取选定的标签并将其存储在新的数组tagsSelectedList中 So, when the user clicks a Tag, I would like to get that tag and push it to tagsSelectedList . 因此,当用户单击标签时,我想获取该标签并将其推送到tagsSelectedList However I am getting an error after I placed an onClick inside the li of the map function. 但是,在将onClick放置在map函数的li内之后,出现了错误。

return (
    <li id={Tag.tagName} class="tag" key={Tag.id} onClick={this.selectTag}>{Tag.tagName}</li>
);

This is the error: 这是错误:

Uncaught TypeError: Cannot read property 'selectTag' of undefined 未捕获的TypeError:无法读取未定义的属性'selectTag'

Component.js: Component.js:

let tags = [
    {id: "1", tagName: "Arts"},
    ...
    ...
    {id: "59", tagName: "Writing"}
}];

var tagsSelectedList = [];

export default class SignUpSubscribeTags extends React.Component {
    constructor(props){
        super(props);
    }

    selectTag = (e) => {
        console.log(e.target.id);
    }

    render() {
        let tagList = tags.map(function(Tag){
            var i = 0;
            return (
                <li id={Tag.tagName} class="tag" key={Tag.id} onClick={this.selectTag}>{Tag.tagName}</li>
            );
        });

        return(
            <div id="signup-process-wrapper-addTags">

                <div id="add_tags">
                    <ul id="tag_list">
                        {tagList}
                    </ul>
                </div>
            </div>
        );
    }
}

However if I remove onClick={this.selectTag} from the return statement of tagList , 但是,如果我从tagList的return语句中删除onClick={this.selectTag}

<li id={Tag.tagName} class="tag" key={Tag.id}>{Tag.tagName}</li>

and place a li with an onClick={this.selectTag} inside the ul, 并在ul中放置一个带有onClick={this.selectTag}li

                <ul id="tag_list">
                    <li id="tagName" class="tag" onClick={this.selectTag}>tagName</li>
                    {tagList}
                </ul>

it works fine! 它工作正常! I get no error. 我没有错。

What am I doing wrong? 我究竟做错了什么? Please help me. 请帮我。 Thank you. 谢谢。

You need to scope this so it references the React component context 您需要this进行范围界定,以便它引用React组件上下文

There are a couple of ways to do this: 有两种方法可以做到这一点:

Option 1: 选项1:

Using bind() 使用bind()

render() {
    let tagList = tags.map(function(Tag){
        var i = 0;
        return (
            <li id={Tag.tagName} class="tag" key={Tag.id} onClick={this.selectTag}>{Tag.tagName}</li>
        );
    }.bind(this));

    return(
        <div id="signup-process-wrapper-addTags">

            <div id="add_tags">
                <ul id="tag_list">
                    {tagList}
                </ul>
            </div>
        </div>
    );
}

Option 2: 选项2:

You can use the ES6 arrow function to scope it 您可以使用ES6箭头功能来确定范围

    let tagList = tags.map((Tag) => {
        var i = 0;
        return (
            <li id={Tag.tagName} class="tag" key={Tag.id} onClick={this.selectTag}>{Tag.tagName}</li>
        );
    });

Option 3: 选项3:

The map function also takes a second argument that specifies what this refers to map函数还采用了第二个参数, this参数指定了this所指的是

    let tagList = tags.map(function(Tag) {
        var i = 0;
        return (
            <li id={Tag.tagName} class="tag" key={Tag.id} onClick={this.selectTag}>{Tag.tagName}</li>
        );
    }, this);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM