Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get global state #729

Closed
wants to merge 2 commits into from
Closed

Get global state #729

wants to merge 2 commits into from

Conversation

tcper
Copy link

@tcper tcper commented Jul 6, 2020

Checklist
  • npm test passes
  • tests are included
  • documentation is changed or added
  • commit message follows commit guidelines
Description of change
  • As a global message bus, I think onGlobalStateChange is not enough.
  • Add convenient get global state data

@kuitos
Copy link
Member

kuitos commented Jul 6, 2020

@dbkillerf6

@kuitos
Copy link
Member

kuitos commented Jul 6, 2020

IMO getGlobalState is not necessary, you could use onGlobalStateChange(state => {}, true) to simulate getGlobalState instead, who combined getter and observer into one ,inspired by rxjs Subject.subscribe design philosophy.

@tcper
Copy link
Author

tcper commented Jul 6, 2020

IMO getGlobalState is not necessary, you could use onGlobalStateChange(state => {}, true) to simulate getGlobalState instead, who combined getter and observer into one ,inspired by rxjs Subject.subscribe design philosophy.

Saw it, I think it's doable,
But it is redundancy, fold a getter function into a closure.

@kuitos
Copy link
Member

kuitos commented Jul 6, 2020

But it is redundancy, fold a getter function into a closure.

Not agreed, it is a common design in reactive programming with my knowledge.

@tcper
Copy link
Author

tcper commented Jul 6, 2020

But it is redundancy, fold a getter function into a closure.

Not agreed, it is a common design in reactive programming with my knowledge.

A simple stage, click button pop up a bubble

onClick = () => {
  message.info(getCurrentGlobalState());
}

Your way

onClick = () => {
  onGlobalStateChange(state => {
    message.info(state);
  }, true);
}

I think we need more readable code more than great philosophy.
Without your inform, I didn't even notice this immediate fire thing.

@kuitos
Copy link
Member

kuitos commented Jul 6, 2020

Keep acquisition and subscription consistently is basic design principle in reactive programming, not such a great philosophy imo, it can help the right way practice of single source of truth and avoid the abuse of global state.

@dbkillerf6
Copy link
Contributor

dbkillerf6 commented Jul 20, 2020

@kuitos
I think @tcper needs an API to get the global state synchronously. In theory, this behavior can be allowed if it is only reading.
But even so, I recommend subscribing the global data to sub-app and then reading the data in the app if your global state contains page rendering data.

@fengxianqi
Copy link

fengxianqi commented Aug 12, 2020

I think it is necessary to add a getGlobalState api. Sub-app can easily get father's store when mount instead of using onGlobalChange.

@wibed
Copy link

wibed commented Nov 25, 2020

I too would prefer a synchronous store access method.
or can someone explain how I can export state directly?

something like

const test = onGlobalState(state => { return state.test }, true)

because at the moment it is looking like

function testGet(){
  let test: any
  onGlobalState(state => { test = state.test }, true)
  return test
}

@yuki070
Copy link

yuki070 commented Mar 5, 2021

Let's see my solutions.

https://github.com/yuki070/qiankun-redux

I use redux to make the global state instead of initGlobalState. Of course, this also can be do in qiankun itself.

@YuhuiRainie
Copy link

I vote for "getGlobalState"

@kuitos
Copy link
Member

kuitos commented Apr 24, 2021

closed via we will not recommend global state api at next major version

@kuitos kuitos closed this Apr 24, 2021
@A-floweR-y
Copy link

But it is redundancy, fold a getter function into a closure.

Not agreed, it is a common design in reactive programming with my knowledge.

A simple stage, click button pop up a bubble

onClick = () => {
  message.info(getCurrentGlobalState());
}

Your way

onClick = () => {
  onGlobalStateChange(state => {
    message.info(state);
  }, true);
}

I think we need more readable code more than great philosophy. Without your inform, I didn't even notice this immediate fire thing.

@kuitos I think getGlobalState sounds better.

The first time I used qiankun, I tried to retrieve the state content set by the initGlobalState method through onGlobalStateChange(callback) but found it not executing. I have been searching for a method like getGlobalState or whether globalState is globally mounted. I consulted the documentation and used Google search, but did not find the required information until I visited GitHub and discovered that the onGlobalStateChange method has a second parameter, which when set to true is similar to getGlobalState.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants