question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to extend state in SPA?

See original GitHub issue

I have just created the basic SPA by doing npx apprun --init --spa. Now I want the app’s state to take care of more than just the component name (Home, About, Contact). I may e.g. want to have a counter (+1/-1) on one of the components. In the basic SPA one gets the impression that the component name is the app’s state (state = 'Home';), but right now it seems that it’s not necessarily so. I’m a bit confused.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
yysuncommented, Dec 3, 2019

It is unclear in the documentation that the mounted lifecycle function is only called in the statefull child component.

class Child extends Component {
  state = {} // you can define the initial state
  view = state => <div></div>
  update = {}
  mounted = (props, children) => { ...state, ...props } // this will be called, you can merge props into the state
}

class Parent extends Component {
  state = {} // you can define the initial state
  view = state => <div>
    <Child />
  </div>
  update = {}
  mounted = () => { } // this will NOT be called when component is created using the constructor
}
new Parent().start(document.body); 

However, you can pass the initial state in to the component’s constructor directly:

const init_state = {};
new Parent(init_state).start(document.body); 
1reaction
yysuncommented, Dec 3, 2019

I would suggest creating a module, e.g. global.ts

export let global_state = {
 name: 'xxx'
}

And import it in main.tsx and other components

import { global_state } from './global';

You can also export a function for updating the global state in global.ts

export let global_state = {
  name: 'xxx'
}
export const set_global_state = state => {
  global_state = state;
}

To call the function, import it:

import { set_global_state } from './global';
set_global_state({ ... })

If you want to notify other components, send a global event in the set_global_state function:

import app from "apprun";
export let global_state = {
  name: 'xxx'
}
export const set_global_state = state => {
  global_state = state;
  app.run('@state_updated', state);
}

Other components can subscribe to the events and merge the global state into the local state:

import { app, Component } from "apprun";
class MyComponent extends Component {
  state = {}
  view = state => <div>{state}</div>
  update = {
    '@state_updated': (state, global_state) => {
      return { ...state, global_state}
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Medicaid State Plan Amendments
States also submit SPAs to request permissible program changes, make corrections, or update their Medicaid or CHIP state plan with new ...
Read more >
State Plan Amendment (SPA) | SC DHHS
1, 2023, SCDHHS will amend the South Carolina Title XIX state plan to increase the reimbursement rates for the State Plan covered dental...
Read more >
Disaster relief state plan amendments - MACPAC
States can submit Medicaid disaster relief state plan amendments (SPAs) to simplify the beneficiary enrollment and renewal process such as expanding use of ......
Read more >
Appearance Enhancement Business | Department of State
Under the 'Action' column, click on 'Renew Application', complete the renewal application, including uploading any documentation you are instructed to provide. ...
Read more >
Advancing Postpartum Coverage in Medicaid: Waiver or SPA?
Numerous states have requested section 1115 waivers to extend postpartum coverage for different lengths of time (i.e. six months in the case ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found