简体   繁体   English

将按下的项目插入react-native数组中

[英]Insert the pressed item into an array in react-native

I have a list of tag objects in array ( tagList ), which I am fetching from the server and storing into the state. 我在数组( tagList )中有一个标记对象列表,我正在从服务器中提取该对象并将其存储到状态中。 After a succesfull fetch I am mapping each item inside a ScrollView . 成功获取后,我正在ScrollView映射每个项目。

export default class RegisterTags extends Component {
    constructor(props) {
        super(props)
        this.state = {
            tagList: [],
            selectedTags: [],
        }
    }

    componentWillMount() {
        api.getTagsOnRegister()
            .then((res) => {
                this.setState({
                    tagList: res
                })
            })
    }

    insertTag = (tag) =>{
        this.setState({
            selectedTags: this.state.selectedTags.push(tag)
        })
    }


    render() {
        return(
            <View style={styles.container}>
                <ScrollView contentContainerStyle={styles.ScrollViewContainer}>
                    {this.state.tagList.map((tag) => {
                        return(
                            <View style={styles.tagStyle} key={tag.id} onPress={this.insertTag(tag)}>
                                <Text style={styles.tagText}>{tag.name}</Text>
                            </View>
                        )
                    })}
                </ScrollView>
            </View>
        )
    }
}

What I want to achieve is, when I press on any of the tag, I would like to add that object into the selectedTags array. 我想要实现的是,当我按下任何标签时,我想将该对象添加到selectedTags数组中。 But I am getting error: 但我得到了错误:

Warning: setState(...): Cannot update during an existing state transition (such as within render or another component's constructor). 警告:setState(...):在现有状态转换过程中(例如在render或另一个组件的构造函数中)无法更新。 Render methods should be a pure function of props and state; 渲染方法应该纯粹是道具和状态的函数; constructor side-effects are an anti-pattern, but can be moved to componentWillMount . 构造函数的副作用是反模式,但是可以将其移至componentWillMount

Possible Unhandled Promise Rejection (id: 0): _this.state.selectedTags.push is not a function TypeError: _this.state.selectedTags.push is not a function 可能的未处理的承诺拒绝(id:0):_this.state.selectedTags.push不是函数TypeError:_this.state.selectedTags.push不是函数

How can I add the pressed tag item into the selectedTags array? 如何将按下的标签项添加到selectedTags数组中?

onPress={this.insertTag(tag)}

Issue : in your render function you are directly calling a function that changes state. 问题 :在render函数中,您正在直接调用更改状态的函数。 Render function should never call any function that updates state. Render函数永远不要调用任何更新状态的函数。

So how to call onPress ? 那么如何调用onPress呢?

Ans: as written below 答:如下

onPress = {() => {this.insertTag(tag) }}

Will code work now? 代码现在可以工作了吗? Answer is no. 答案是否定的。

You have to put your views inside TouchableHighlight and move onPress method from View to TouchableHighlight . 您必须将视图放入TouchableHighlight并将onPress方法从View移至TouchableHighlight Then hopefully your code works. 然后希望您的代码可以工作。 I am assuming everything is setup property. 我假设一切都是安装程序属性。

~~~~~~EDIT 1~~~~~~~~ ~~~~~~编辑1 ~~~~~~~~~

What't the difference between onPress={this.insertTag(tag)} and onPress = {() => {this.insertTag(tag) }} What't之间的差 onPress={this.insertTag(tag)} onPress = {() => {this.insertTag(tag) }}

Every thing inside curly braces are expressions in react jsx . 花括号中的所有东西都是react jsx中的表达式 Now onPress={this.insertTag(tag)} evaluates the expression inside curly braces and assign it to onPress property, and in your case this.insertTag happens to update state. 现在onPress={this.insertTag(tag)}对花括号内的表达式求值并将其分配给onPress属性,在您的情况下, this.insertTag恰好会更新状态。

While onPress = {() => {this.insertTag(tag) }} , on evaluating curly braces returns a function, but doesn't call that function. onPress = {() => {this.insertTag(tag) }} ,在计算花括号时会返回一个函数,但不会调用该函数。 And when onPress event is triggered than that function is called. 当触发onPress事件时,将调用该函数。

Google "Arrow Function" for more. 谷歌“箭头功能”更多。

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

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