title: Reconciler

Taro’s runtime includes DOM, BOM, React-compatible layer, Vue-compatible layer, etc., and different platforms or development frameworks may require intrusive customization of the Taro runtime.

To decouple, we refer to the concept of React Reconciler, where the runtime can be customized externally by providing a custom hostConfig configuration object.

In case the configuration items of hostConfig do not meet the requirements and need to be extended, you can submit a PR to Taro ~

hostConfig Configuration

appendChild (parent, child)

Triggered when DOMNode calls appendChild method.

Parameters Type Description
parent DOMNode Parent node
child DOMNode / TextElement Nodes appended to the parent node

removeChild (parent, child, oldChild)

Triggered when DOMNode calls replaceChild method.

Parameters Type Description
parent DOMNode Parent node
child DOMNode / TextElement Replace the oldChild with a new node
oldChild DOMNode / TextElement Replaced original nodes

insertBefore (parent, child, refChild)

Triggered when DOMNode calls the insertBefore method.

Parameters Type Description
parent DOMNode Parent node
child DOMNode / TextElement Inserted nodes
refChild DOMNode / TextElement Insert before this node

removeAttribute (element, qualifiedName)

Triggered when DOMElement invokes the removeAttribute method.

Parameters Type Description
element DOMElement Current operating element
qualifiedName string Specify the name of the attribute to be removed from the element

setAttribute (element, qualifiedName, value)

Triggered when DOMElement invokes the setAttribute method.

Parameters Type Description
element DOMElement Current operating element
qualifiedName string String of the property name
value Value of the property / new value

prepareUpdateData (data, page)

Triggered every time the Taro DOM tree is updated, before calling the mini program setData.

Parameters Type Description
data DataTree Taro DOM tree data structure for setData
page TaroRootElement Page root element

appendInitialPage (data, page)

Taro DOM tree initialization, triggered before the first call to the mini program setData. Executed immediately after the call to prepareUpdateData.

Parameters Type Description
data DataTree Taro DOM tree data structure for setData
page TaroRootElement Page root element

getLifecyle (instance, lifecyle)

Called when the lifecycle of the mini program page is triggered.

Parameters Type Description
instance Instance Page Instance
lifecyle string Lifecycle function name

Needs to return function or function[], indicating the function to be executed.

Example:

  1. // Default value.
  2. // Takes the corresponding lifecycle method directly from the user-written page instance
  3. getLifecyle (instance, lifecyle) {
  4. return instance[lifecyle]
  5. }
  6. // In React
  7. // the mini program triggers onShow, which calls the user-written componentDidShow
  8. // the mini program triggers onHide, calling the user-written componentDidHide
  9. getLifecyle (instance, lifecycle) {
  10. if (lifecycle === 'onShow') {
  11. lifecycle = 'componentDidShow'
  12. } else if (lifecycle === 'onHide') {
  13. lifecycle = 'componentDidHide'
  14. }
  15. return instance[lifecycle]
  16. }

onTaroElementCreate (tagName, nodeType)

Triggered when DOMElement is constructed.

Parameters Type Description
tagName string The tag name of the currently created element
nodeType NodeType The node type of the currently created element
nodeType Description
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 COMMENT_NODE
7 PROCESSING_INSTRUCTION_NODE
9 DOCUMENT_NODE

getPathIndex (indexOfNode)

Triggered when DOMNode gets the path property.

Parameters Type Description
indexOfNode number The subscript of the current node in the children list of the parent node

Needs to return a string value representing the array subscript when the mini program is setData by path.

Example:

  1. // Default value
  2. getPathIndex (indexOfNode) {
  3. return `[${indexOfNode}]`
  4. }
  5. // Baidu smart program does not require [] Parcels
  6. getPathIndex (indexOfNode) {
  7. return indexOfNode
  8. }

getEventCenter(Events)

Triggered when Taro.eventCenter initializes its value.

Parameters Type Description
Events Constructor for Taro Event Center

Needs to return an instance of the Taro event center, which will be assigned to Taro.eventCenter.

Example:

  1. // Default value:
  2. getEventCenter (Events) {
  3. return new Events()
  4. }
  5. // In the Alipay mini program
  6. // Priority is given to removing created event center instances from the mini program global object my to avoid problems when subpackaging.
  7. getEventCenter (Events) {
  8. if (!my.taroEventCenter) {
  9. my.taroEventCenter = new Events()
  10. }
  11. return my.taroEventCenter
  12. }

initNativeApi (taro)

Triggered when referencing the @tarojs/taro package.

Parameters Type Description
taro Taro Object

Example:

  1. initNativeApi (taro) {
  2. // Add getApp method to Taro object
  3. taro.getApp = getApp
  4. }