作为Route prop传递的函数-TypeError:this.props.handleOnDeleteInApp不是函数

时间:2020-07-28 10:11:55

标签: reactjs react-router react-router-dom

我正在尝试访问在App.js中声明的函数,该函数作为prop在route上传递。但是,ListItem呈现的组件(route)包含了

TypeError: this.props.handleOnDeleteInApp is not a function

相关代码: app.js

export default class App extends Component {

  constructor(props) {
    super(props);
    this.state = {
      items: [{"_id":{"$oid":"5f1fda0169e133382277a4ef"},"title":"Blabla","description":"adfadfd211233","__v":0}]
    }

    this.handleOnUpdate = this.handleOnUpdate.bind(this);
    this.handleOnDelete = this.handleOnDelete.bind(this);
  }

  componentDidMount() {
    ...

  }

  ...

  handleOnDelete() {
    console.log('Deleting from app.js')
    Axios.get('http://localhost:4200/items/delete/'+this.props.obj._id)
    .then(console.log('Deleted'))
    .catch(err => console.log(err))
    this.handleOnUpdate();
  }

  render() {
    return (
      <Router>
        <Navbar bg="dark" variant="dark">
          <Navbar.Brand>To Do App</Navbar.Brand>
          <Nav.Link as={Link} to="/add">Add</Nav.Link>
          <Nav.Link as={Link} to="/index">List</Nav.Link>
        </Navbar>

        <Container style={{ marginTop: 50 }}>
          <Switch>
                    ...

            <Route path='/index' render={(props) => <ListItem {...props} handleOnUpdate={this.handleOnUpdate} hanldeOnDeleteInApp={this.handleOnDelete} items={this.state.items} />} />
          </Switch>
        </Container>

      </Router>
    );
  }
}

ListItem.js

export default class ListItem extends Component {

    constructor(props) {
        super(props);

        this.handleOnUpdate = this.handleOnUpdate.bind(this);
        this.handleExport = this.handleExport.bind(this);
        this.handleOnDelete = this.handleOnDelete.bind(this);
    }

    ...

    itemRow = () => {
        var that = this;
        return this.props.items.map(function (object, i) {
            return <ItemRow obj={object} key={i} onDelete={that.handleOnDelete} onUpdate={that.handleOnUpdate} />
        })
    }
    ...

    handleOnUpdate() {
        this.props.handleOnUpdate();
    }

    handleOnDelete() {
        this.props.handleOnDeleteInApp();
    }


    render() {
        return (
            <Container>
                <table className="table table-striped">
                    <thead>
                        <tr>
                            <td>Title</td>
                            <td>Description</td>
                        </tr>
                    </thead>
                    <tbody>
                        {this.itemRow()}
                    </tbody>
                </table>
                <ExportItem handleExport={this.handleExport} />
            </Container>
        )
    }
}

ItemRow.js

class ItemRow extends Component {
    constructor(props) {
        super(props);
        this.onDelete = this.onDelete.bind(this);
    }

    onDelete = () => {
        this.props.onDelete();
    }

    render() {
        return (
            <tr>
                ...                   
                    <button onClick={this.onDelete} className="btn btn-danger">Delete</button>
            </tr>
        );
    }
}

export default ItemRow;

当我的功能道具从ItemRow正常传递到ListItem时,我导致相信将功能作为道具传递给Route很有帮助。

有人在这里发现错误吗?

1 个答案:

答案 0 :(得分:2)

路线输入错误 <Route path='/index' render={(props) => <ListItem {...props} handleOnUpdate={this.handleOnUpdate} hanldeOnDeleteInApp={this.handleOnDelete} items={this.state.items} />} />

hanldeOnDeleteInApp应该是handleOnDeleteInApp