Component
A component in Resquare is a piece of UI that you can reuse in other components or render to the user, it can receive props (i.e., input), store state, and return corresponding elements.
#
Component vs. Element?TODO
#
Declare componentYou can declare a component without props, with props, or with optional props.
#
Component without props#
Component with propsWe strongly recommend you use data class as props class.
#
Component with optional propsThe way to create a component with optional props input is to provide a default props factory. When the component is called without props input, the default props will be used.
#
Node typeYou can return a node as a result of the component, and there have few types of node:
- Element (div)
- Component
- List of node
- Children list of node
- Null
#
Element#
Component#
List of nodeYou can return a list of nodes as a single node. If you are nesting a list inside another list, all of them will be flattened as 1 list at the final stage.
#
Component key in listIf you are returning a component as a list element, you must provide a key
for Resquare to recognize the element, or you will receive a warning from Resquare.
Key can be any unique string or number identifier, such as id. If there have no way to provide a unique identifier for it, you can also use an index as an identifier, but it may cause problems when the item order is changed.
key is important!
If you are really interested in why we need a key
, you can check React document.
#
Children ListThe children list is basically the same as the list, but the size of it should be fixed and no key is needed.
tl;dr, Which list I should use?
- Use
childrenOf
when you are going to make a fixed nodes list. - Use
+list
when you are going to render a data list as nodes.
โ Don't:
๐ Do:
โ Don't:
๐ Do:
#
NullThe null node is useful when you want to hide an element in the list by condition,
it is basically equal to childrenOf()
, which means it will render nothing.
Remember that null itself is not a node, so you must use +
to wrap the nullable node.