key是未定义的反应

时间:2017-12-06 15:16:33

标签: reactjs key undefined

当我在本地主机中播放此代码时,它告诉我"键未定义"

import React from 'react';
import PropTypes from 'prop-types'

class LineTable extends React.Component{

supprimerLine = key => {
    const newLines = {...this.state.newLines};
    newLines[key] = null;
    this.setState({newLines})
};
render(){
return(
    <tr>
        <td>{this.props.details.colone1}</td>
        <td>{this.props.details.colone2}</td>
        <td>{this.props.details.colone3}</td>
        <td><button onClick={() => this.props.supprimerLine(key)}>
                                    supprimer</button></td>
    </tr>)};
static propTypes = {
    supprimerLine: PropTypes.func.isRequired};}
export default LineTable

我认为这是因为我没有导入密钥,但我不知道如何导入密钥

2 个答案:

答案 0 :(得分:1)

你有两点需要纠正。

  1. supprimerLine在同一个班级LineTable内定义。

    所以,this.props.supprimerLine(key)不是调用它的正确方法。它会抛出一个错误。

    正确的方法是this.supprimerLine(key)

    在构造函数中添加 this.supprimerLine = this.supprimerLine.bind(this);

  2. 您尚未在当前上下文或类中的任何位置定义key。您需要先定义它。

答案 1 :(得分:0)

键未定义,因为您的函数上下文不存在。你期待什么&#34;关键&#34;包含?

.filter('piemel', function(){
return function(items, value){
    var returnList = [];

    if(items){
        for(x in items){
            var tag = Object.keys(items[x])[0].toLowerCase();

            if(value){
                value = value.toLowerCase();
                if( tag.indexOf(value) != -1){
                    returnList.push(items[x]);
                }
            }
            else{
                returnList.push(items[x]);
            }
        }
    }

    return returnList;
};
})

也应该是未定义的,因为你的方法存在于你的类而不是你的道具中。