數(shù)據(jù)傳值大概無非是這幾種情況。
1.父傳子
2.子傳夫
3.跨層級組件傳值
4.路由傳值(什么情況都可以用~但是涉及一些長的或者隱私的不建議用)
我這個人對傳值這塊真的有時候迷迷糊糊的,所以這次剛好復(fù)習(xí)的時候整理一下傳值缆瓣。
父傳子
這個是最簡單也是最常用的傳值了虹统。
前提條件就是父組件要引用子組件车荔!
通常是通過props和ref傳值和方法
props
父組件:
import './App.css';
import Chid from './Chid.jsx'
import React, { Component } from 'react'
export default class extends Component {
constructor(props) {
super(props)
}
fun() {
//這里是傳的方法
console.log('this is function')
}
render() {
return (
<div className="App">
<Chid fun={this.fun} aa='value' />
</div>
)
}
}
子組件:
import React, { Component } from 'react'
export default class Chid extends Component {
constructor(props) {
super(props)
//通過props傳遞的function 執(zhí)行fun()
this.props.fun.bind(this)()
}
render() {
return (
<div>
{this.props.aa}
</div>
)
}
}
通過ref傳值
父組件可以通過ref選擇到子組件的function族吻,如果需要子組件的值則用return
父組件:
import './App.css';
import Chid from './Chid.jsx'
import React, { Component } from 'react'
export default class extends Component {
constructor(props) {
super(props)
}
componentDidMount() {
let x = this.foo.myFun() //輸出:我是被傳的function
console.log(x)
}
render() {
return (
<div className="App">
<Chid ref={(foo) => { this.foo = foo }} />
</div>
)
}
}
子組件:
import React, { Component } from 'react'
export default class Chid extends Component {
myFun() {
console.log('我是被傳的function')
}
render() {
return (
<div>
{this.props.aa}
</div>
)
}
}
子傳父
父組件通過props向子組件傳入一個方法呼奢,子組件在通過調(diào)用該方法握础,將數(shù)據(jù)以參數(shù)的形式傳給父組件悴品,父組件通過該方法使用數(shù)據(jù)。
父組件:
import './App.css';
import Chid from './Chid.jsx'
import React, { Component } from 'react'
export default class extends Component {
constructor(props) {
super(props)
}
getData = (e) => {
console.log(e)
}
render() {
return (
<div className="App">
<Chid getData={this.getData} />
</div>
)
}
}
子組件:
import React, { Component } from 'react'
export default class Chid extends Component {
constructor(props) {
super(props)
this.state = {
chidData: [1, 2, 3, 4]
}
}
render() {
const { chidData } = this.state
return (
<div>
<button onClick={() => { this.props.getData(chidData) }}>
{chidData}
</button>
</div>
)
}
}
利用props callback通信定枷,父組件傳遞一個 callback 到子組件届氢,當(dāng)事件觸發(fā)時將參數(shù)放置到 callback 帶回給父組件退子。
父組件:
import './App.css';
import Chid from './Chid.jsx'
import React, { Component } from 'react'
export default class extends Component {
constructor(props) {
super(props)
}
callback = (value) => {
//得到子組件傳值value
console.log(value)
}
render() {
return (
<div className="App">
<Chid callback={this.callback} />
</div>
)
}
}
子組件:
import React, { Component } from 'react'
export default class Chid extends Component {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
}
handleChange = (e) => {
// 在此處將參數(shù)帶回
this.props.callback(e.target.value)
}
render() {
return (
<div>
<input type='text' onChange={this.handleChange} />
</div>
)
}
}
跨層級組件之間
使用 Context作為中間載體傳值。避免了在每一個層級手動的傳遞 props 屬性荐虐,適合于多層級的時候使用
官方文檔
首先:新建 Context.js
const { Consumer, Provider } = React.createContext(null)
//創(chuàng)建 context 并暴露Consumer(使用者)和Provide(提供者)
export { Consumer, Provider }
父元素(提供者)使用:
import './App.css';
import Chid from './Chid.jsx'
import React, { Component } from 'react'
//引入 取名 Provider
import { Provider } from './context'
export default class extends Component {
constructor(props) {
super(props)
}
render() {
return (
<div className="App">
<Provider value={'this.state.info'}>
<div>
<p>{'this.state.info'}</p>
<Chid />
</div>
</Provider>
</div>
)
}
}
子組件:
import React, { Component } from 'react'
import { Consumer } from './context'
import GrandSon from './Grandson'
export default class Chid extends Component {
constructor(props) {
super(props)
}
render() {
return (
<Consumer>
{(info) => (
// 通過Consumer直接獲取父組件的值
<div>
<p>父組件的值:{info}</p>
<GrandSon />
</div>
)}
</Consumer>
)
}
}
孫子組件:
import React from 'react'
import { Consumer } from './context'
class GrandSon extends React.Component {
//
constructor(props) {
super(props)
}
// 如果要用值的話惜犀,先使用 static 這個類屬性來初始化 contextType向拆,然后通過this.context獲取值
static contextType = Consumer;
componentDidMount() {
//this.context是Consumer info的值浓恳,輸出和顯示一致颈将。
console.log(this.context)
}
render() {
return (
<Consumer>
{(info) => (
// 通過 Consumer 中可以直接獲取組父組件的值
<div>
<p>組父組件的值:{info}</p>
</div>
)}
</Consumer>
)
}
}
export default GrandSon
路由傳值
最經(jīng)典的就是使用params傳值 需安裝react-router-dom
形式:/:xx (xx就是要傳值的參數(shù))
怎么用呢晴圾?來看以下幾步。
首先
路由配置:
<Route exact path="/index/:name" component={Index} />
//跳轉(zhuǎn)地址就是/index/xx
1.js跳轉(zhuǎn):
this.props.history.push('/index/mika')
2.標(biāo)簽跳轉(zhuǎn):
引入Link標(biāo)簽 import { Link } from 'react-router-dom'
使用Link標(biāo)簽跳轉(zhuǎn) <Link to="/index/mika">this is link</Link>
跳轉(zhuǎn)頁面接收值:this.props.match.params.name
其中name就是傳的參數(shù)值人乓,自己定義了什么就叫什么名字勤篮。
還有一種query傳參
1.js跳轉(zhuǎn):
this.props.history.push({
pathname: '/index',
query: { name: 'mika', age: 18 }
})
在要跳轉(zhuǎn)的組件上綁定onClick就可以使用。query為一個對象碰缔,攜帶的是需要傳的參數(shù)。
不需要提前引入金抡,也不需要路由設(shè)置,正常配置就可以腌且。
跳轉(zhuǎn)頁面接收值:this.props.location.query
簡單的說一下2個傳參的區(qū)別:
1.params傳遞顯示參數(shù),query傳遞不顯示參數(shù)铺董,query相對于params來說較安全一點
2.params傳值頁面刷新數(shù)據(jù)還在巫击,而query傳值頁面刷新數(shù)據(jù)會消失。
3.params傳值只能傳一個精续,query可以傳多個對象。
那有沒有數(shù)據(jù)傳參不顯示驻右、刷新地址欄數(shù)據(jù)也不會小時的方法呢堪夭?有啊!
state 傳參
傳參的方法和query差不多橘蜜,屬性不一樣。
不需要路由配置
1.js跳轉(zhuǎn):this.props.history.push({pathname:'/index',state:{name:'mima'}})
2.標(biāo)簽跳轉(zhuǎn) <Link to={{path:'/index',state:{name:'mika'}}}>XX</Link>
跳轉(zhuǎn)頁面接收值: this.props.location.state.name
以上暫時就這些了沒包括一些傳值的包计福,如果需要可以自己去了解