1

I'm looking for a solution for my problem. I'm using a library called unstated (https://github.com/GitbookIO/unstated). It makes a global state for an reactjs app. The problem is: I can't use a state made on a function X in my function Y. In this case, I created my state in App.js and wanna access it in my DivCheckBox.js.

App.js:

    import './App.css';
import DivCheckBox from './components/DivCheckBox/DivCheckBox';
import { Container, useUnstated } from '@gitbook/unstated';

class DadosUnstated extends Container<{
  nodesPadrao: Object,
  edgesPadrao: Object,
  }> {
  state = {
    nodesPadrao: [
      {
        status: 0,
        id:1,
        label: 'MAT001',
        title: 'CALCULO DIFERENCIAL E INTEGRAL I',
        details: "Integrais- impróprias: seqüências séries numéricas. Funções de R em R. Derivadas. Integrais. Aplicações. \"Regras de L'Hospital\".",
        level: 1,
        weight: 90,
        groupId: 1,
        subGroupId: 1,
        slots: [1, 2],
      },
      {
        status: 0, 
        id:50,
        label: 'EEE019',
        title: 'TRABALHO DE CONCLUSAO DE CURSO II',
        details: 'Finalização do projeto elaborado, no projeto final de curso I e apresentação de monografia. Defesa do trabalho perante banca examinadora.',
        level: 11,
        weight: 90,
        groupId: 5,
        subGroupId: 4,
        slots: [1, 2],
      },
      {
        status: 0, 
        id:51,
        label: 'EEE020',
        title: 'ESTAGIO SUPERVISIONADO EM ENGENHARIA DE SISTEMAS',
        details: 'Atividades de treinamento, supervisionadas por um docente do curso, na área de atuação profissional do engenheiro de sistemas.',
        level: 12,
        weight: 165,
        groupId: 5,
        subGroupId: 4,
        slots: [1, 2],
      },
    ],

    edgesPadrao:[
      {
        id: 1,
        from: 1,
        to: 7,
        label: 'MAT001 -> MAT039',
      },
      {
        id: 29,
        from: 32,
        to: 41,
        label: 'ELEXXE -> ELEXXF',
      },
      {
        id: 30,
        from: 44,
        to: 42,
        label: 'ELE090 -> ELEXXG',
      },
      {
        id: 31,
        from: 40,
        to: 41,
        label: 'ELEXXF -> ELE088',
      },
      {
        id: 32,
        from: 30,
        to: 42,
        label: 'ELT080 -> ELEXXG',
      },
      {
        id: 33,
        from: 35,
        to: 40,
        label: 'ELE077 -> ELE088',
      },
      {
        id: 55,
        from: 11,
        to: 33,
        label: 'ELEXXI -> ELEXXJ',
      },
      {
        id: 56,
        from: 47,
        to: 48,
        label: 'ELEXXK -> ELE087',
      },
    ],

  };
 
  increment(id) {
    let aux=this.state.nodesPadrao;
    aux[id-1].status=!aux[id-1].status;
    this.setState({ nodesPadrao: aux });
    console.log(this.state.nodesPadrao);
  }
 
  decrement(id) {
    let aux=this.state.nodesPadrao;
    aux[id-1].status=!aux[id-1].status;
    this.setState({ nodesPadrao: aux });
    console.log(this.state.nodesPadrao);
  }

}

function App() {

  const dados = useUnstated(DadosUnstated);

  return (
    <div>

              {dados.state.nodesPadrao.map(p => {
              return (
                
                <DivCheckBox nomeDisciplina = {p.title} labelDisciplina = {p.label} id = {p.id}/>
                
              )})
              }

            <p>{dados.state.edgesPadrao[3].status} oiiiiii {dados.state.edgesPadrao[0].status}</p>

    </div>
  );
}

export default App;

DivCheckBox.js:

    import React, { Component } from 'react';
import Func from './func'
import { Container, useUnstated } from '@gitbook/unstated';
import DadosUnstated from 'C:/Users/decyf/Desktop/PDEG/pdeg/src/App.js'

function DivCheckBox(props){

    const dados = useUnstated(DadosUnstated);

    dados.nodesPadrao[2].status=12;

    return(
          <div>
          <p id = "pgh">{props.nomeDisciplina}</p>
          <input id="MAT001" class = {props.labelDisciplina} type="checkbox" onClick={() => Func(props.labelDisciplina,props.id)}/>
          </div>
    );
    
}

export default DivCheckBox;

Func.js:

    import { Container, useUnstated } from '@gitbook/unstated';
import DadosUnstated from 'C:/Users/decyf/Desktop/PDEG/pdeg/src/App.js'

function Func(classe,id){

    const dados = useUnstated(DadosUnstated);

    var chb = document.getElementsByClassName(classe);

    if(chb[0].checked){
        console.log("marquei o "+classe);
        
    }else if(!chb[0].checked){
        console.log("desmarquei o "+classe);
        
    }

}

export default Func;

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • If you are referring to "function Y" and it isn't a functional React component then the `useUnstated` React hook isn't a valid hook usage. React hooks are only valid in React functional components or other custom React hooks. You need to convert `Func` into a React component. – Drew Reese Mar 24 '21 at 21:51
  • and then how can I use the state inside of it? @DrewReese – Paulo Henrique Mar 24 '21 at 21:52
  • If by "it" you refer to `Func` then you can pass state to that function, or you can convert `Func` to a React component so it can use the `useUnstated` hook. Not very clear what's going on, considering you don't even use `dados` after declaring it. – Drew Reese Mar 24 '21 at 21:55
  • DivCheckBox is a react component, right? but I still cant read my state (created in app.js) within it. why? should I pass it thru parameter? Unstated is not very well documented, so I couldnt understand how I access it from another component well. @DrewReese – Paulo Henrique Mar 24 '21 at 22:01
  • `DivCheckBox`, yes, it appears to be a React component. Is that where you have an issue? What is `DadosUnstated` that you are importing in `DivCheckBox.js` and passing to the `useUnstated` hook? It looks like the `App` component that was default exported. Is `useUnstated` supposed to consume a React component or was it supposed to consume *some* state object/context/etc? – Drew Reese Mar 24 '21 at 22:09
  • I only imported DadosUnstated in case of test. I just didnt know what I was doing. All I want is to access that state (declared as const dados = useUnstated(dadosUnstated)). – Paulo Henrique Mar 24 '21 at 22:14
  • DivCheckBox is where I'm having the issue. I try to access const dados there (which is what Unstated does, but it's not working. I try to change my state within DivCheckBox, but it only works within where I created it (App.js)) – Paulo Henrique Mar 24 '21 at 22:15
  • look here's a different library but it looks smiilar: https://www.npmjs.com/package/unstated . it shows how to access a state within the component you created it. but I wanna know how to access it from ANOTHER component – Paulo Henrique Mar 24 '21 at 22:17
  • Like I said, I think you need to pass the "state container" `DadosUnstated` to the `useUnstated` hook, you are passing your `App` component, which you default exported/imported. – Drew Reese Mar 24 '21 at 22:27
  • holy shit @DrewReese it worked!! but hey, how can I change Func.js to a react component without changing too much code? – Paulo Henrique Mar 24 '21 at 23:17
  • relax it worked. THANK YOU!!! – Paulo Henrique Mar 25 '21 at 00:01
  • What is `Func` supposed to actually do, and where is it called? It looks to just do some console logging, it may be easier to rename it `useFunc` is consider it a React hook. It would still need to follow the rules of hooks though (called only from functional components or other custom hooks, etc...). – Drew Reese Mar 25 '21 at 15:15

1 Answers1

0

Issue

I think you've mixed up what you are exporting from App.js and importing for use in DivCheckBox.

In App.js:

export default App;

In DivCheckBox.js:

import DadosUnstated from 'C:/Users/decyf/Desktop/PDEG/pdeg/src/App.js';

...

const dados = useUnstated(DadosUnstated);

Here DadosUnstated is default imported from App.js, which is the App React component, not the DadosUnstated container that was defined.

Solution

I think you probably exported DadosUnstated as a names export

export DadosUnstated;

So you should import it as a named import

import { DadosUnstated } from 'C:/Users/decyf/Desktop/PDEG/pdeg/src/App.js';

I believe it will now reference the object you are expecting it to reference, i.e. the container, and the hook should now work.

const dados = useUnstated(DadosUnstated);
Drew Reese
  • 165,259
  • 14
  • 153
  • 181