【问题标题】:Function passed as Route prop - TypeError: this.props.handleOnDeleteInApp is not a function作为 Route prop 传递的函数 - TypeError: this.props.handleOnDeleteInApp is not a function
【发布时间】:2020-07-28 10:11:55
【问题描述】:

我正在尝试访问在App.js 中声明的函数,该函数已作为route 上的道具传递。但是,route 渲染的组件 (ListItem) 比较

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 是有问题的。

有人发现这里的错误吗?

【问题讨论】:

  • 你把它作为你的 listItem.js 组件中的道具你检查了反应开发工具吗?

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


【解决方案1】:

路线中的拼写错误 &lt;Route path='/index' render={(props) =&gt; &lt;ListItem {...props} handleOnUpdate={this.handleOnUpdate} hanldeOnDeleteInApp={this.handleOnDelete} items={this.state.items} /&gt;} /&gt;

hanldeOnDeleteInApp 应该是handleOnDeleteInApp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多