赋值后未定义onPress on TouchableOpacity

时间:2020-05-23 21:45:13

标签: react-native touchableopacity

我的本​​机应用程序上有这个循环:

         for(var i = 0; i<sets.length;i++)  {
            console.log('sets[i].setName = '+sets[i].setName);
            arrViews.push(
                 <TouchableOpacity 
                    onPress={()=> editSetBtn(sets[i].setName, sets[i].questions)}
                    lesQuestions={sets[i].questions} 
                    key={i}>
                     <Text>
                       {sets[i].setName}
                     </Text>
                 </TouchableOpacity>);
            }

当我运行它时,我的控制台中确实有sets[i].setName = Set name,但是当我单击touchableOpcatity时出现此错误:

TypeError: undefined is not an object (evaluating 'sets[i].setName')

createReactClass$argument_0.touchableHandlePress
    TouchableOpacity.js:264:45
touchableHandlePress
    [native code]:0
TouchableMixin._performSideEffectsForTransition
    Touchable.js:880:34
_performSideEffectsForTransition
    [native code]:0
TouchableMixin._receiveSignal
    Touchable.js:779:44
_receiveSignal
    [native code]:0
TouchableMixin.touchableHandleResponderRelease
    Touchable.js:490:24
touchableHandleResponderRelease
    [native code]:0
forEach
    [native code]:0
callFunctionReturnFlushedQueue
    [native code]:0

有人知道为什么吗?

编辑和修复: 所以我将循环更改为此,并且有效:

            for(var i = 0; i<sets.length;i++){
            console.log('sets[i].setName = '+sets[i].setName);
            let name = sets[i].setName;
            let questions = sets[i].questions;
            arrViews.push(<TouchableOpacity onPress={()=> editSetBtn(name, questions)} lesQuestions={sets[i].questions} key={i}><Text>{sets[i].setName}</Text></TouchableOpacity>);
        }

但是我仍然需要知道为什么会这样?

0 个答案:

没有答案