Search code examples
javascriptnativescript

Error when using addChild twice in one function in NativeScript


I am trying to dynamically create stackLayouts and add them to the existing scrollView but got an error:

JS ERROR TypeError: scrollView.addChild is not a function. (In 'scrollView.addChild(stackLayout)', 'scrollView.addChild' is undefined)

but by using console.log it sees both of them:

StackLayout(10) ScrollView<dates>@diary/diary-page.xml:4:7;

My code is:

input.forEach(function(entry) {
    const scrollView = page.getViewById("dates");
    var stackLayout = new StackLayout();
    stackLayout.className = 'date';
    var label = new Label();
    label.text = entry.Date;
    stackLayout.addChild(label);
    console.log(stackLayout + ' ' + scrollView);
    scrollView.addChild(stackLayout);
});

Can anybody help with this strange behaviour? Thank you


Solution

  • Found the reason here: How to nest elements in dynamically created ScrollView?

    "ScrollView is actually an implantation of ContentView so the right way to set it's content is by doing:

    scrollview.content = stacklayout

    Note: Only can only set a single content view. That's the reason there is not addChild method as it suggests multiple children are supported."