Giter VIP home page Giter VIP logo

Comments (7)

quisido avatar quisido commented on May 18, 2024

Can you provide a minimal implementation of this so that I can be sure I understand what is happening in your use case?

from reactn.

EskelCz avatar EskelCz commented on May 18, 2024

Strange thing, I've made the minimal implementation with basically the same code and it's working.
https://snack.expo.io/@eskel/globaltoasts

I guess it's not a reactn issue then, sorry.

from reactn.

EskelCz avatar EskelCz commented on May 18, 2024

@CharlesStover I've examined it a little more and it seems that the origin is indeed in reactn, the enqueueForceUpdate call. I guess snack has a different YellowBox settings or their forked RN behaves differently. I see it in React Native 0.59.2. Here is a screenshot of the warning:

screenshot of warning

So the question is, do you support calls from unmounted components?

from reactn.

quisido avatar quisido commented on May 18, 2024

I would like to support your use case where possible. Can you provide a minimal implementation that causes this error? You mentioned the code sandbox provided above works.

from reactn.

EskelCz avatar EskelCz commented on May 18, 2024

@CharlesStover I found out that the sandbox (Expo snack) has it's own implementation of console and is not showing all React Native warnings. :( I'll let them now.

The code works both in sandbox and in production code, it's just the warning that forceUpdate should not be run in my case.

I've made and tested the minimal implementation, (it shows the warning) but it takes some time to build and install, if you don't have much experience with RN.
Here: https://github.com/EskelCz/reactn-global-test/blob/master/App.js

from reactn.

quisido avatar quisido commented on May 18, 2024

Thank you for the example. Why are you extending Component if it's not a React Component? If it's just for the global member variables and methods, you can import those directly from reactn, the same way you instantiated the state.

class ToastAPI extends Component {
  showToast (text) {
    const toastList = this.global.toasts
    toastList.push({ text: 'Some toast' })
    this.setGlobal({ toasts: toastList })
  }
}
export const Toasts = new ToastAPI()

can be:

import { setGlobal } from 'reactn';
export function showToast(text) {
  setGlobal(global => ({
    toasts: global.toasts.concat([ { text: 'Some toast' } ]),
  });
};

If you want to maintain your toast API as a class, just don't extend Component:

import { setGlobal } from 'reactn';
class ToastAPI {
  showToast(text) {
    setGlobal(global => ({
      toasts: global.toasts.concat([ { text: 'Some toast' } ]),
    });
  }
};
export const Toasts = new ToastAPI();

Does this solve your problem while meeting your use case? My interpretation of the problem is that you were wanting ReactN's global state functionality outside of a React Component.

from reactn.

EskelCz avatar EskelCz commented on May 18, 2024

Awesome, that did it. :) At first I was using vanilla class but then I didn't know how to get the global state inside it. I must admit I don't really find the callback pattern very readable setGlobal(global => ({. But it works and doesn't cause the warning. Thanks a lot!

from reactn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.