简体   繁体   English

React.js按钮未从另一个文件调用函数

[英]React.js button not calling function from another file

I am trying to call the function deleteToDo from toDo.js but when I click the button it doesn't console.log anything. 我试图从toDo.js调用函数deleteToDo,但是当我单击按钮时,它没有console.log任何东西。 It doesn't give me any errors either. 它也没有给我任何错误。 Any help would be appreciated it's probably a pretty easy question but i'm very new to react. 任何帮助将不胜感激,这可能是一个非常简单的问题,但我是新来的反应。

app.js: app.js:

import React, { Component } from 'react';
import './App.css';
import ToDo from "./components/ToDo.js"

class App extends Component {
  constructor(props){
    super(props);
    this.state = {
      todos: [
        {description: 'Walk the cat', isCompleted: false},
        {description: 'Throw the dishes away', isCompleted: false},
        {description: 'Buy new dishes', isCompleted: false}
      ],
       newTodoDescription: ''
    };
  }

  deleteToDo(){
    console.log("test");
  }

  render() {
    return (
      <div className="App">
       <ul>
        <ToDo/>
       </ul>
      </div>
    );
  }
}

export default App;

todo.js : todo.js:

import React, { Component } from "react";

class ToDo extends Component{
  render(){
    return(
      <li>
        <button onClick={this.props.deleteToDo}>Delete</button>
      </li>
    );
  }
}

export default ToDo;

Extend your app.js. 扩展您的app.js。 You should pass the reference of your function via props. 您应该通过道具传递函数的引用。

<ToDo deletetodo={this.deleteTodo} />

Hope this helps! 希望这可以帮助!

It is because you are not passing the deleteToDo function to the ToDo component. 这是因为您没有将deleteToDo函数传递给ToDo组件。

// app.js
  deleteToDo = ()=>{
    console.log("test");
  }

  render() {
    return (
      <div className="App">
       <ul>
        <ToDo deleteToDo={this.deleteToDo}/>
       </ul>
      </div>
    );
  }

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

相关问题 如何从React.js中的另一个文件实际启动函数 - How to actually initiate a function from another file in React.js React.js从另一个按钮调用按钮单击 - React.js calling a button click from a different button 在React.js中从父组件调用子组件函数 - Calling Child component function from Parent component in React.js 在 React.js 中将值从一个 function 传递到另一个 function - Passing a value from one function to another function in React.js 将 React.js 中的 function 从一个 function 组件传递到另一个组件 - Passing function in React.js from one function component to another 在单击事件的反应函数组件中定义的调用函数形成另一个函数组件 - React.js - Calling function defined within a react function component on a click event form another function component - React.js React.js / CRA,如何从另一个文件调用或导入javaScript函数? - React.js / CRA, How to call or import a javaScript function from another file? 如何从另一个React.js文件调用函数? - How do I call a function from another React.js file? 点击按钮从 React.js function 返回图像 - Return image from React.js function on button click 从 React.js 中的另一个 class 组件调用 function - Call a function from another class component in React.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM