React-native: Resizing native component in native code does not work.

Created on 28 Dec 2015  路  8Comments  路  Source: facebook/react-native

I have a custom native component which needs to change its size when some click is provided to it.
I use following api but with no effect -

onClick(){
    getLayoutParams().height = 200
    getLayoutParams().width = 200
   requestLayout();
}

but with no effect, it is not able to change its size.
How to circumvent such behaviour?

Locked

Most helpful comment

You can manually trigger layout.

See ReactPicker.java or ReactToolbar.java for example.

 @Override
  public void requestLayout() {
    super.requestLayout();

    // The spinner relies on a measure + layout pass happening after it calls requestLayout().
    // Without this, the widget never actually changes the selection and doesn't call the
    // appropriate listeners. Since we override onLayout in our ViewGroups, a layout pass never
    // happens after a call to requestLayout, so we simulate one here.
    post(measureAndLayout);
  }

All 8 comments

I am using react-native v0.13rc with android.

Hey jayshah123, thanks for reporting this issue!

React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.

  • If you don't know how to do something or something is not working as you expect but not sure it's a bug, please ask on StackOverflow with the tag react-native or for more real time interactions, ask on Discord in the #react-native channel.
  • If this is a feature request or a bug that you would like to be fixed, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
  • We welcome clear issues and PRs that are ready for in-depth discussion. Please provide screenshots where appropriate and always mention the version of React Native you're using. Thank you for your contributions!

Particularly subviews are also not able to change their size, even if the exposed native component is of fixed size or with flex:1 and styled from JS,

onClick(){
    subview.getLayoutParams().height = 200
    subview.getLayoutParams().width = 200
    subview.requestLayout();
    requestLayout();
}

If somebody happens to know the reason or has faced similar problem I would be glad to know the same.
For know it is not a very primary concern for me so closing the issue.

You can manually trigger layout.

See ReactPicker.java or ReactToolbar.java for example.

 @Override
  public void requestLayout() {
    super.requestLayout();

    // The spinner relies on a measure + layout pass happening after it calls requestLayout().
    // Without this, the widget never actually changes the selection and doesn't call the
    // appropriate listeners. Since we override onLayout in our ViewGroups, a layout pass never
    // happens after a call to requestLayout, so we simulate one here.
    post(measureAndLayout);
  }

@realaboo Thanks so much for this. I spent days banging my head against the wall but this solved it!

@realaboo Thanks! It's insane that it's not a part of the documentation.

@realaboo thank you so much!

Was this page helpful?
0 / 5 - 0 ratings