1

When I use the reactn library ( that manage and facilitate hooks) to get my initial global state that I assigned with setGlobal its not setting anything. When I try to get It says undefined.

I have used this library before and it usually is just a matter of importing like : import React from "reactn"; import { setGlobal } from "reactn";

And then just set the globals.

import React from "reactn";
import { setGlobal } from "reactn";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";

ReactDOM.render(<App />, document.getElementById("root"));


setGlobal({
    test: "test",
    something: { attr1: "something", attr2: "something", attr3: "something"}
});


serviceWorker.register();
import React, { useEffect, useGlobal } from "reactn"; 

export default function Engine(props){

    const [something, setSomething] = useGlobal("something");
    const [test] = useGlobal("test");

    console.log(something, test)

    useEffect( ()=>{
        console.log(something, test)
    } );


    return(
<div></div>);
}

I expected the values that I'm supposed to get instead of undefined undefined

bluefang05
  • 21
  • 2
  • I just created a [codesandbox](https://codesandbox.io/s/dazzling-mclaren-xlkt2) and it's working fine. How is `Engine` used in your `App`? – maazadeeb Oct 04 '19 at 03:05

1 Answers1

3

Issue is that global state is being set (setGlobal) after rendering the App (ReactDOM.render).

Move it above the render and it should work.

ckedar
  • 1,859
  • 4
  • 7