這篇“React數據共享useContext怎么實現”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“React數據共享useContext怎么實現”文章吧。
問題:
ReferenceError: Cannot access 'Context' before initialization This error happened while generating the page. Any console logs will be displayed in the terminal window.
引用錯誤 , 不能在初始化之前訪問Context , 在生成頁面的時候就已經發生。在Shell控制臺也有顯示輸出。
我嘗試過很多的辦法, 例如:換用引用的方式、多個Provider的位置調整,甚至只保留一個 , 依然無法解決。
后來我試試可能組建聲明的類型問題。
我平時對于寫組建的方式比較隨意。
最喜歡的一種方式就是
import { useState , createContext} from 'react'
import Me from './me'
export const MyContext = createContext({});
export default function Demo(){
const [say , setSay] = useState('');
return (
<MyContext.Provider value={{say , setSay}}>
<div>father</div>誰在講話 {say}
<Me />
</FatherContext.Provider>
)
}React.FC是函數式組件寫法,是在TypeScript使用的一個泛型,FC就是FunctionComponent的縮寫,事實上React.FC可以寫成React.FunctionComponent ( 我對這種寫法感覺太過于冗余 )
import React, { createContext, FunctionComponent, useState } from 'react'
import Me from './me'
interface DemoProps {
say: string;
setSay: React.Dispatch<React.SetStateAction<boolean>>;
demoString?:string;
}
const defaultDemoProps: DemoProps = {
isDay: true,
setIsDay: (day) => day
};
export const MyContext = createContext<DemoProps>({...defaultDemoProps});
const Demo: React.FC<DemoProps> = ({ children, ...props }) => {
const { say : propIsSay } = props;
const [ isSay, setSay ] = useState(propIsDay)
return <MyContext.Provider value={{ say,setSay}}>
<Me />
</MyContext.Provider>
}
export default Demo;還有很多習慣使用class components
import React, { createContext, PureComponent } from 'react'
import Me from './me'
export const MyContext = createContext({})
export default class Demo extends PureComponent {
state = {
say:true,
}
setSay ()=>{
let say = !this.state.say
this.setState({
say
});
}
render() {
return(
<MyContext.Provider value={{ say,setSay}}>
<Me />
<MyContext.Provider>
)
}
}這是三種的構造方式
createContext 函數返回的有3個屬性分別是 Provider ( 提供者 )、Customer( 消費者 )、displayName ( 貌似用不到 )
import React, { Context, PureComponent } from 'react';
import {MyContext} from '../components/data';
import Memo from '../components/classDemo';
export const MyContext = React.createContext()
export default class CurstomerDemo extends PureComponent {
static contextType = MyContext // 重點是這句 用來指定
constructor(props) {
super(props);
}
handleClick = () => {
console.log (this.context) // 獲取上下文 Provider 屬性
}
render() {
return (
<div>
<button>Provider</button>
<button onClick={this.handleClick}>Customer</button>
</div>
)
}
}
import React, { useState ,useContext, createContext} from 'react'
import {MyContext} from './Demo'
function useCountContext(CounterContext) {
const context = useContext(MyContext) //重點這句話,用來獲取指定的上線文Context
if (!context) {
throw new Error('useCountContext must used within Context.Provider')
}
return context
}
export default function Me(props){
let context = useCountContext(MyContext)
let {say , setSay} = context
return (
<div>
me
<button onClick={()=>{
setSay(say + ',同志們好')
}}>come from grandpa Provier {say}</button>
</div>
)
}其實關鍵的還是用函數方式來接受函數的Provider , 類組件來接受類組件的Provider。保證構造的一致。
PS:useContext 我個人覺得對于小型項目還是非常的不錯,但是對于復雜的數據,他的分層意識還是不夠清晰。thunk、saga、mobx 都在一定程度上在分層上更適合context。當然你也可以對context更進一步封裝。適合自己的才是最好!
以上就是關于“React數據共享useContext怎么實現”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。