一、子组件给父组件传参
1-1子组件的属性接收父组件传递过来的方法
//父组件
import React from 'react';
import Title from './components/Title'
class App extends React.Component {
constructor(props){
super(props);
this.state = {
msg:"hello world"
}
}
render() {
return (
<div>
<Title
// 1.子组件的属性接收父组件传递过来的方法
deleteItem = {this.handleDelete.bind(this)}
msg={this.state.msg}></Title>
</div>
)
}
handleDelete(id){
console.log(id)
}
}
export default App;
1-2在子组件调用方法,向组件传参
//子组件
import React from 'react';
class Title extends React.Component {
constructor(props){
super(props)
}
render (){
return (
<h1 onClick={this.handleClick.bind(this,"11313")}>{this.props.msg}</h1>
)
}
handleClick=(id)=>{
// 2.在子组件调用方法,向组件传参
this.props.deleteItem(id)
}
}
export default Title;