React-属性传值(传事件)
程序员文章站
2024-02-26 15:39:52
...
Person.js
import React from 'react';
const Person=(props)=>{
return(
<div>
<p>
我是{props.name},今年{props.age}岁
</p>
<button onClick={props.myclick}>子组件的点击</button>
</div>
)
}
export default Person
App.js
import React, {Component} from 'react';
import logo from './logo.svg';
import './App.css';
import Person from './Person/Person'
class App extends Component{
state={
persons:[{name:'小仙女',age:'18'},{name:'小玉',age:'18'},{name:'家璇',age:'18'}],
}
change=(newname)=>{
let that=this;
//动态的更改值 类似于小程序语法
this.setState({
persons:[
{name:newname,age:'18'},
{name:'小玉',age:'18'},
{name:'家璇',age:'18'}
],
})
}
render(){
return(
<div className="App">
<Person name={this.state.persons[0].name} age={this.state.persons[0].age} myclick={this.change.bind(this,'小可爱小可爱')}/>
</div>
)
}
}
export default App;