Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-111344

Suspense equivalent for QML

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • None
    • None
    • All

    Description

      React has a concept of delaying a component render for when
      its dependencies are ready, named Suspense [1].

      In QML what would be the equivalent / alternative?

      The rather naive approach would be to use a `Loader`  in a component like this [2] 

      import QtQuick
      import QtQuick.Controls
      Pane {id:root
          property bool completed: false
          property var network_data: {'hello': 'world'}
          Timer {id:timer
              interval: 500;
              onTriggered: root.completed = true
          }  
          Button{
              text: 'fetch'
              onClicked: timer.start()
          }
          Suspense{
              width: 200; height: 200;
              anchors.centerIn: parent;
              test: root.completed
              fallback: Loading{}
              delegate: NeedsNetworkData{network_data: root.network_data}
          }
      }
      // Loading.qml
      import QtQuick
      AnimatedImage{
          anchors.centerIn: parent;
          source: "https://mir-s3-cdn-cf.behance.net/project_modules/disp/04de2e31234507.564a1d23645bf.gif"
      }
      // NeedsNetworkData.qml
      import QtQuick
      Rectangle{
          required property var network_data
          color: "grey"
          Text{
              anchors.centerIn:parent;
              text: JSON.stringify(network_data.hello)
          }
      }
      
      // Suspense.qml
      import QtQuick
      Loader {
          id: root
          required property bool test
          required property Component fallback
          required property Component delegate
          
          sourceComponent: test? delegate: fallback
      }
      

      However I seek for a better solution for

       1. I instantiated the component here:

      delegate: NeedsNetworkData{network_data: root.network_data}

       2. React has no `delegate` property you just dump what ever component you want (that is 'Suspenseable') inside the Suspense component, i.e:

      <Suspense fallback={<Loading />}>
        <Biography />
        <Panel>
          <Albums />
        </Panel>
      </Suspense>

      3. In react you don't need to have the `test` property I used above.

       

       
      The goal of suspense is to delay initialization of the component that depends on the (not arrived yet) data in order to avoid null-checks while sustaining the declarative style of the language.
      i.e:

      Text{
          text: data.field.otherfield
      }

      without suspense would look like

      Text{
          text: data? data.field.otherfield: "😨"  
      }

      A further discussion has been made on SO here

        

      [1] -  https://beta.reactjs.org/reference/react/Suspense
      [2]https://stephenquan.github.io/qmlonline/?zcode=AAAEa3ichVPLbtswELzrKwhfnBwsOX6kgIKiaN1LisJtmtwDmlxJrClSIVdJDUH/HpKWFdmNWx4kcrmPmd2hKCttkNzhXS3YNhJHx3ilFRotbfSTKiCN4KnRGiPiVmV0BQZ3ZKO1JEyXlQQEnpKMSgvHLs/UEAX4os32kVOkKWnGBUipxykZO6vk4zZEPIgSTKiDfhdsfgmFYJ6pTMlyOr3pzVo9GJHnYHxdjyzucZCPBE29x9ESEv5fakStmj4c4Q86ABkgK8aDpCvpuPuUAURskRq8uNynCt/72lagLLylehEci5TMHDpSgMgL3B96B6pYoY2NGXgqtyolFTVufzNAY/GURX/pmio3lG1T8l1TLlTetP0dBwk5RUjJGoDb9b7RX12fm+Omh9xDU9txaqMoSQ6Z46dSnugg+qxE6Srw25LmHet/E7K6NswhGhWIlU2TpBRmYucTxtWEZfEGChcPHkziVPIbGD6WmtcSbMKFrZLpgsMM5lez+WI5/RAvrxf0is/m14vlJotzkY06zKeM3wMf/XLpqcplh9zAUy2cZM7rM7gxLbVxDHIDu9FenU4vzfmJ/j1QL69v9z/WTkLGdVZku4thmTi8gcu3IXhGB229y8SPyD+QECI60Z8hFd6lF9WZ+5UTmVYOcS+u/zoelBYcB3PuPdJQ8NNAkn3uNnoFjD9q7A==

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            nir nir benlulu
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes