[英]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.