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.

State Value is not preserved when ComponentsConfiguration.isEndToEndTestRun = true;

See original GitHub issue

Version

***implementation ‘com.facebook.litho:litho-core:0.21.0’ implementation ‘com.facebook.litho:litho-widget:0.21.0’ compileOnly ‘com.facebook.litho:litho-annotations:0.21.0’

annotationProcessor 'com.facebook.litho:litho-processor:0.21.0'

// SoLoader
implementation 'com.facebook.soloader:soloader:0.5.1'
// For testing
testImplementation 'com.facebook.litho:litho-testing:0.21.0'***

Issues and Steps to Reproduce

Enable the flag ComponentsConfiguration.isEndToEndTestRun = true in MainActivity. State values are not preserved with ComponentsConfiguration.isEndToEndTestRun = true

Expected Behavior

State values should be preserved with ComponentsConfiguration.isEndToEndTestRun = true

Link to Code

MainActivity
Add ComponentsConfiguration.isEndToEndTestRun = true

MyColumnComponentSpec.java : Create 3 TextWithState Components with state values

@LayoutSpec
public class MyColumnComponentSpec {

  @OnCreateLayout
  static Component onCreateLayout(ComponentContext c) {
    return Column.create(c)
        .child(TextWithState.create(c).initial(1))
        .child(TextWithState.create(c).initial(2))
        .child(TextWithState.create(c).initial(3))
        .build();
  }
}

TextWithStateSpec.java Same state value is present in all 3 TextWithState components even though I pass different state value "in @Prop initial "

@LayoutSpec
public class TextWithStateSpec {

  @OnCreateInitialState
  static void OnCreateInitialState(
      ComponentContext c,
      StateValue<Integer> counter,
      @Prop int initial) {
    counter.set(initial);
  }

  @OnCreateLayout
  static Component onCreateLayout(
      ComponentContext c,
      @State int counter) {
    return Text.create(c).text("count = "+counter).textSizeDip(30)
        .build();

  }
}

Screenshots : When ComponentsConfiguration.isEndToEndTestRun = false State Values are maintained individually flagfalse

When ComponentsConfiguration.isEndToEndTestRun = true Same state value for all 3 text components. flagtrue


Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
anushreeagcommented, Dec 11, 2018

Thank you @astreet for looking into this quickly and fixing it.

1reaction
anushreeagcommented, Nov 30, 2018

While debugging this, I found https://github.com/facebook/litho/blob/master/litho-core/src/main/java/com/facebook/litho/Component.java#L600

When I remove the check “!ComponentsConfiguration.isEndToEndTestRun” - UI is rendered properly and state values are preserved individually . Since the key is not registered for every component when ComponentsConfiguration.isEndToEndTestRun = true , Key is duplicated and state value is also duplicated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to keep React component state between mount/unmount?
Since you can't keep the state in the component itself when it unmounts, you have to decide where else it should be saved....
Read more >
Preserving and Resetting State - React Docs Beta
React keeps track of which state belongs to which component based on their place in the UI tree. You can control when to...
Read more >
How To Manage State on React Class Components
You'll see the cart update, but not the price. The total state value is preserved during the update. This value is only preserved...
Read more >
useState vs. useRef: Similarities, differences, and use cases
The useState Hook enables the development of component state for ... state is preserved over render as long as you do not update...
Read more >
How To Use and Not Use State - React Training
This doesn't happen with setting state in function components with ... Just be sure to take care of preserving all the state when...
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