嗨,我是jsx的初学者,我正面临一个简单的错误,但我无法解决它 我已经在我的应用js中编写了一个简单的删除功能,而我却陷入了这个错误。
这是我的App.js文件
import React, { Component } from 'react';
import Essai from './Essai.js';
import AddEssai from './addEssai.js';
class App extends Component {
state = {
essais : [
{name : 'pek', age: 30, sexe : 'f', id:'1'},
{name : 'plop', age: 31, sexe : 'fh', id:'2'},
{name : 'pekkrure', age: 35, sexe : 'h', id:'3'}
]
}
deleteEssai = (id) =>{
console.log('fonction ok')
}
render() {
return (
<div className="App">
<h1>My first react app</h1>
<Essai essai ={this.state.essais}/>
<AddEssai deleteEssai = {this.deleteEssai} addEssai = {this.addEssai} />
</div>
)
};
}
export default App;
这是我的组件
import React, { Component } from 'react';
const Essai = (props) => {
//console.log(this.props.age)
const { essai } = props;
const essaiList = essai.map( essai => {
return(
<div className= "essai" key = {essai.id}>
<div>Name: {essai.name} </div>
<div>Age: {essai.age} </div>
<div>sexe: {essai.sexe} </div>
<button onClick = {() => {deleteEssai(essai.id)}}>DELETE</button>
</div>
)
})
return(
<div className = "essaiList">
{essaiList};
</div>
)
}
export default Essai
我总是会收到此错误
./ src / Essai.js 13:43:未定义'deleteEssai'no-undef
如果有人可以花些时间指导我,那将非常感谢!!!
答案 0 :(得分:1)
实际上您在子组件中使用了deleteEssai
,但是从不将其作为道具传递给它,因此由于那里没有这样的东西,它会引发未定义的错误。您只需要将它作为道具传递给子组件。
赞:
<Essai deleteEssai={this.deleteEssai} essai={this.state.essais}/>
然后然后,您还需要在子组件中进行破坏。
const { essai, deleteEssai } = props;
答案 1 :(得分:0)
您应该看的两件事:
deleteEssai
作为对<Essai />
中组件<App />
的支持。import React, { Component } from 'react';
import Essai from './Essai.js';
import AddEssai from './addEssai.js';
class App extends Component {
state = {
essais : [
{name : 'pek', age: 30, sexe : 'f', id:'1'},
{name : 'plop', age: 31, sexe : 'fh', id:'2'},
{name : 'pekkrure', age: 35, sexe : 'h', id:'3'}
]
}
deleteEssai = (id) =>{
console.log('fonction ok')
}
render() {
return (
<div className="App">
<h1>My first react app</h1>
{/* ->>> Pass here*/}
<Essai essai={this.state.essais} deleteEssai={this.deleteEssai}/>
<AddEssai deleteEssai = {this.deleteEssai} addEssai = {this.addEssai} />
</div>
)
};
}
export default App;
<Essai />
中从道具中使用它:import React, { Component } from 'react';
const Essai = (props) => {
//console.log(this.props.age)
// Desctructure 'deleteEssai' from props
const { essai, deleteEssai } = props;
const essaiList = essai.map( essai => {
return(
<div className= "essai" key = {essai.id}>
<div>Name: {essai.name} </div>
<div>Age: {essai.age} </div>
<div>sexe: {essai.sexe} </div>
<button onClick = {() => {deleteEssai(essai.id)}}>DELETE</button>
</div>
)
})
return(
<div className = "essaiList">
{essaiList};
</div>
)
}
export default Essai;