Home,js
import React, {Component} from 'react';
import News from "./News";
class Home extends Component {
constructor(props) {
super(props);
this.state={
text:'我是默认值'
}
}
dataFun=(text)=>{
console.log(text)
this.setState({
text
})
}
render() {
return (
<div>
home---
<p>{this.state.text}</p>
<News name="你好" fufun={this.dataFun}/>
</div>
);
}
}
export default Home;
news,js
import React, {Component} from 'react';
import PropTypes from 'prop-types';
class News extends Component {
constructor(props) {
super(props);
this.state={
num:1111,
ziText:"我是子组件的值"
}
}
fun(){
this.setState({
num:222
})
}
render() {
return (
<div>
News-----{this.props.name}-----{this.state.num}
<button onClick={this.fun.bind(this)}>哈哈</button>
<button onClick={this.props.fufun.bind(this,this.state.ziText)}>发送</button>
</div>
);
}
}
News.propTypes = {};
export default News;
app.js
import React from 'react';
import './App.css';
import Home from './components/Home.js'
function App() {
return (
<div className="App">
你好
<Home></Home>
</div>
);
}
export default App;