1.父组件的值传递给子组件
import React, { Component } from 'react'
import "./Pay.less"
export default class Pay extends Component {
state={
list:['111','222','333']
}
render() {
return (
<div>
<Acc
chlidlist={this.state.list}
/>
<Aay></Aay>
</div>
)
}
}
class Acc extends Component {
render() {
return (
<div>
{
this.props.chlidlist.map((item,index)=>
<button className='a' key={index} >{item}</button>
)
}
</div>
)
}
}
class Aay extends Component {
render() {
return (
<div>Pay</div>
)
}
}
2.父组件的方法,怎么传递给子组件
import React, { Component } from 'react'
import "./Pay.less"
export default class Pay extends Component {
state={
list:['111','222','333']
}
render() {
return (
<div>
<Acc childClick={this.chlidfn}
chlidlist={this.state.list}
/>
<Aay></Aay>
</div>
)
}
chlidfn=()=>{
console.log(1);
}
}
class Acc extends Component {
render() {
return (
<div>
{
this.props.chlidlist.map((item,index)=>
<button className='a' key={index} onClick={()=>{
this.cnfn()
}}>{item}</button>
)
}
</div>
)
}
cnfn=(index)=>{
this.props.childClick()
}
}
class Aay extends Component {
render() {
return (
<div>Pay</div>
)
}
}
3.子传给父了
import "./Pay.less"
export default class Pay extends Component {
state={
list:['111','222','333']
}
render() {
return (
<div>
<Acc childClick={this.chlidfn}
chlidlist={this.state.list}
/>
<Aay></Aay>
</div>
)
}
chlidfn=(id)=>{
console.log(id);
}
}
class Acc extends Component {
render() {
return (
<div>
{
this.props.chlidlist.map((item,index)=>
<button className='a' key={index} onClick={()=>{
this.cnfn(index)
}}>{item}</button>
)
}
</div>
)
}
cnfn=(index)=>{
this.props.childClick(index)
}
}
class Aay extends Component {
render() {
return (
<div>Pay</div>
)
}
}
4. 子组件的方法传递给父组件