ReactN setGlobal()/ useGlobal()对我不起作用

时间:2019-10-10 04:52:53

标签: reactjs

我正在尝试在一页中设置Global()变量和对象,并在下一页中对其进行检索

StackOverflow.js

import React, {setGlobal, useGlobal, useEffect} from 'reactn'

setGlobal({
    myVar: '',
    myObj: {}
})

const StackOverflow = () => {
    const [myVar, setMyVar] = useGlobal('myVar')
    const [myObj, setMyObj] = useGlobal('myObj')

    useEffect(() => {
        setMyVar('Hello World')
        setMyObj({name: 'Mr Magoo'})       
    }, [])

    useEffect(() => {
        console.log("MyVar: ", myVar, "\nMyObj: ", myObj)
    }, [myVar, myObj])

    return (
        <div style={{padding: 10}}>
            <h2><a href="/stackOverflow2">Click me</a></h2>
        </div>
    )
}

export default StackOverflow

StackOverflow2.js

import React, {useGlobal} from 'reactn'


const StackOverflow2 = () => {
    const [myVar] = useGlobal('myVar')
    const [myObj] = useGlobal('myObj')

    return (
        <div style={{padding: 10}}>
            <h2>StackOverflow2</h2>
            <div>My Var: {myVar}</div>
            <div>My Obj: {myObj.name}</div>
        </div>
    )
}

export default StackOverflow2

我希望能够在一页上设置Global()变量/对象,并在第二页上使用Global()变量/对象。我什么也没得到。

0 个答案:

没有答案