|
| 1 | +Getting Started |
| 2 | +=============== |
| 3 | + |
| 4 | +Let's break down the following example: |
| 5 | + |
| 6 | +.. example:: slideshow |
| 7 | + :linenos: |
| 8 | + |
| 9 | +Since it's likely a lot to take in at once, we'll break it down piece by piece: |
| 10 | + |
| 11 | +.. literalinclude:: /examples/slideshow.py |
| 12 | + :lineno-start: 4 |
| 13 | + :lines: 4-5 |
| 14 | + :linenos: |
| 15 | + |
| 16 | +The ``idom.component`` decorator creates a :ref:`Component <Stateful Components>` |
| 17 | +constructor whose "renderer" is the function below it. To create a Component instance |
| 18 | +we call ``Slideshow()`` with the same arguments as its render function. The render |
| 19 | +function of a Component returns a data structure that depicts a user interface, or in |
| 20 | +more technical terms a Document Object Model (DOM). We call this structural |
| 21 | +representation of the DOM a `Virtual DOM`__ (VDOM) - a term familiar to those who work |
| 22 | +with `ReactJS`_. In the case of ``Slideshow`` it will return a VDOM representing an |
| 23 | +image which, when clicked, will change. |
| 24 | + |
| 25 | +__ https://reactjs.org/docs/faq-internals.html#what-is-the-virtual-dom |
| 26 | + |
| 27 | +.. literalinclude:: /examples/slideshow.py |
| 28 | + :lineno-start: 6 |
| 29 | + :lines: 6 |
| 30 | + :linenos: |
| 31 | + |
| 32 | +The :func:`~idom.core.hooks.use_state` function is a :ref:`Hook <Life Cycle Hooks>`. |
| 33 | +Calling a Hook inside a Component's render function (one decorated by ``idom.component``) |
| 34 | +adds some local state to it. IDOM will preserve the state added by Hooks between calls |
| 35 | +to the Component's render function. |
| 36 | + |
| 37 | +The ``use_state`` hook returns two values - the **current** state value and a function |
| 38 | +that let's you update that value. In the case of ``Slideshow`` the value of the |
| 39 | +``use_state`` hook determines which image is shown to the user, while its update |
| 40 | +function allow us to change it. The one required argument of ``use_state`` is the |
| 41 | +**initial** state value. |
| 42 | + |
| 43 | +.. literalinclude:: /examples/slideshow.py |
| 44 | + :lineno-start: 8 |
| 45 | + :lines: 8,9 |
| 46 | + :linenos: |
| 47 | + |
| 48 | +The function above will get added as an event handler to the resulting view. When it |
| 49 | +responds to an event it will use ``set_state`` (the update function returned by the |
| 50 | +``use_state`` Hook) to change which image is shown to the user. Calling the update |
| 51 | +function will schedule the Component to be re-rendered. That is, the Component's render |
| 52 | +function will be called again, and its new result will be displayed. |
| 53 | + |
| 54 | +.. note:: |
| 55 | + |
| 56 | + Even handlers like ``next_image`` which respond to user interactions receive an |
| 57 | + ``event`` dictionary that contains different information depending on the type of |
| 58 | + event that occurred. All supported events and the data they contain are listed |
| 59 | + `here`__. |
| 60 | + |
| 61 | +__ https://reactjs.org/docs/events.html |
| 62 | + |
| 63 | +.. literalinclude:: /examples/slideshow.py |
| 64 | + :lineno-start: 11 |
| 65 | + :lines: 11-16 |
| 66 | + :linenos: |
| 67 | + |
| 68 | +Finally we come to the end of the ``Slideshow`` body where we return a model for an |
| 69 | +``<img/>`` element that draws its image from https://picsum.photos. Our ``next_image`` |
| 70 | +event handler has been added to the image so that when an ``onClick`` event occurs we |
| 71 | +can respond to it. We've also added a little bit of CSS styling to the image so that |
| 72 | +when the cursor hovers over the image it will become a pointer so it appears clickable. |
| 73 | +The returned model conforms to the `VDOM mimetype specification`_. |
| 74 | + |
| 75 | +.. literalinclude:: /examples/slideshow.py |
| 76 | + :lineno-start: 20 |
| 77 | + :lines: 20 |
| 78 | + :linenos: |
| 79 | + |
| 80 | +This last step runs a simple web server that will send the layout of elements defined in |
| 81 | +our ``Slideshow`` to the browser and receive any incoming events from the browser via a |
| 82 | +WebSocket. To display the layout we can navigate to http://localhost:8765/client/index.html. |
| 83 | + |
| 84 | +.. note:: |
| 85 | + |
| 86 | + See the :ref:`Examples` section for more info on the ways to display your layouts. |
| 87 | + |
| 88 | + |
| 89 | +.. Links |
| 90 | +.. ===== |
| 91 | +
|
| 92 | +.. _VDOM event specification: https://github.com/nteract/vdom/blob/master/docs/event-spec.md |
| 93 | +.. _VDOM mimetype specification: https://github.com/nteract/vdom/blob/master/docs/mimetype-spec.md |
| 94 | +.. _ReactJS: https://reactjs.org/docs/faq-internals.html |
| 95 | +.. _React Hooks: https://reactjs.org/docs/hooks-overview.html |
0 commit comments