React组件通信--props
生活随笔
收集整理的這篇文章主要介紹了
React组件通信--props
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
React組件通信
1、props接收數(shù)據(jù)
//index.js import App from "./App";ReactDOM.render(<App name='傳遞數(shù)據(jù)' age={19}color={[1,2,3]}fn={()=>{console.log('函數(shù)')}}tag={<p>段落</p>}/>,document.getElementById("root") );第一種方式:函數(shù)組件props通過(guò)參數(shù)接收
//app.js function App(props) {function myClick(e) {console.log(props);}return (<div className="App"><button onClick={myClick}>按鈕</button></div>); } export default App;第二種方式:類組件通過(guò)this.props.直接訪問(wèn)
class App extends React.Component {render() {return (<div><span>props: {this.props.name}</span></div>);} } export default App;特點(diǎn):
2、父?jìng)髯?/h3>
使用props:
//父組件 import Children from "./Children";class App extends React.Component {state = {data: "父組件state數(shù)據(jù)",};render() {return (<div><span>props: {this.props.age}</span><div style={{ color: "red", border: "1px solid #f00" }}>子組件:<Children data={this.state.data} /></div></div>);} } export default App; //子組件 import React from "react";class Children extends React.Component {render() {return (<div><h3> childrens </h3><p>數(shù)據(jù):{this.props.data}</p></div>);} }export default Children;3、子傳父
利用回調(diào)函數(shù),父組件提供回調(diào),子組件調(diào)用
//父組件 import Children from "./Children"; class App extends React.Component {state = {data: "父組件state數(shù)據(jù)",data2: "",};getChildData = (val) => {console.log("val", val);this.setState({data2: val,});};render() {return (<div><span>props: {this.props.age}</span><div style={{ color: "red", border: "1px solid #f00" }}>子組件:<Children data={this.state.data} getChildData={this.getChildData} />子組件數(shù)據(jù):{this.state.data2}</div></div>);} }export default App; //子組件 class Children extends React.Component {sendData = () => {this.props.getChildData("子組件傳遞數(shù)據(jù)");};render() {return (<div><h3> childrens </h3><p onClick={this.sendData}>數(shù)據(jù):{this.props.data}</p></div>);} }export default Children;4、兄弟組件傳值
用父級(jí)做中間件,子傳父,父再傳子
//children1 import React from "react";class Children extends React.Component {sendData = () => {this.props.getChildData("子組件傳遞數(shù)據(jù)");};render() {return (<div><h3> childrens </h3><p onClick={this.sendData}>點(diǎn)擊children1</p>數(shù)據(jù):{this.props.data}</div>);} }export default Children; //Children2 import React from "react"; class Children2 extends React.Component {render() {return (<div>顯示兄弟children組件的數(shù)據(jù):{this.props.data}</div>);} }export default Children2; //App 父組件 import Children from "./Children"; import Children2 from "./Children2"; class App extends React.Component {state = {data: "父組件state數(shù)據(jù)",data2: "",};getChildData = (val) => {console.log("val", val);this.setState({data2: val,});};render() {return (<div><span>props: {this.props.age}</span><div style={{ color: "red", border: "1px solid #f00" }}>子組件:<Children data={this.state.data} getChildData={this.getChildData} />父組件顯示子組件數(shù)據(jù):{this.state.data2}子組件2:<Children2 data={this.state.data2} /></div></div>);} }export default App;其實(shí)都是用了props
5.children屬性
- children屬性:表示組件標(biāo)簽的子節(jié)點(diǎn),當(dāng)組件標(biāo)簽有子節(jié)點(diǎn)時(shí),props就會(huì)有該屬性。
- 與props屬性一樣,值可以是任意值(文本、dom、甚至函數(shù))
6.props深入
總結(jié)
以上是生活随笔為你收集整理的React组件通信--props的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 烧烤的发源地在哪里?
- 下一篇: 母鸡为什么吃自己的蛋?