{"id":"jsdotlua/react-reconciler","name":"react-reconciler","scope":"jsdotlua","platform":"roblox","description":"Mirrored from the Wally registry.","version":"17.2.1","latest":"17.2.1","versions":["17.0.1","17.0.2","17.0.3","17.0.4","17.1.0","17.2.0","17.2.1"],"license":"MIT","licenseRating":"safe","licenseCaveats":[],"licenseVerified":true,"dependencies":{"jsdotlua/luau-polyfill":{"version":"^1.2.6","alias":"luau-polyfill"},"jsdotlua/promise":{"version":"^3.5.0","alias":"promise"},"jsdotlua/react":{"version":"^17.2.1","alias":"react"},"jsdotlua/scheduler":{"version":"^17.2.1","alias":"scheduler"},"jsdotlua/shared":{"version":"^17.2.1","alias":"shared"}},"integrity":"605901d4a866c446efb5cbd627e9704e654fe68fa8d2a7a11691312c3b0f1f8f","likes":0,"downloads":0,"install":"forest install jsdotlua/react-reconciler","url":"https://forest.dev/p/roblox/jsdotlua/react-reconciler","files":"https://api.forest.dev/ai/package/roblox/jsdotlua/react-reconciler/files","readme":"# `react-reconciler`\n\n**NOTE:** This README is copied directly from React upstream.\n\nThis is an experimental package for creating custom React renderers.\n\n**Its API is not as stable as that of React, React Native, or React DOM, and does not follow the common versioning scheme.**\n\n**Use it at your own risk.**\n\n## Usage\n\n```js\nconst Reconciler = require('react-reconciler');\n\nconst HostConfig = {\n  // You'll need to implement some methods here.\n  // See below for more information and examples.\n};\n\nconst MyRenderer = Reconciler(HostConfig);\n\nconst RendererPublicAPI = {\n  render(element, container, callback) {\n    // Call MyRenderer.updateContainer() to schedule changes on the roots.\n    // See ReactDOM, React Native, or React ART for practical examples.\n  }\n};\n\nmodule.exports = RendererPublicAPI;\n```\n\n## Practical Examples\n\nA \"host config\" is an object that you need to provide, and that describes how to make something happen in the \"host\" environment (e.g. DOM, canvas, console, or whatever your rendering target is). It looks like this:\n\n```js\nconst HostConfig = {\n  createInstance(type, props) {\n    // e.g. DOM renderer returns a DOM node\n  },\n  // ...\n  supportsMutation: true, // it works by mutating nodes\n  appendChild(parent, child) {\n    // e.g. DOM renderer would call .appendChild() here\n  },\n  // ...\n};\n```\n\n**For an introduction to writing a very simple custom renderer, check out this article series:**\n\n* **[Building a simple custom renderer to DOM](https://medium.com/@agent_hunt/hello-world-custom-react-renderer-9a95b7cd04bc)**\n* **[Building a simple custom renderer to native](https://medium.com/@agent_hunt/introduction-to-react-native-renderers-aka-react-native-is-the-java-and-react-native-renderers-are-828a0022f433)**\n\nThe full list of supported methods [can be found here](https://github.com/facebook/react/blob/main/packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js). For their signatures, we recommend looking at specific examples below.\n\nThe React repository includes several renderers. Each of them has its own host config.\n\nThe examples in the React repository are declared a bit differently than a third-party renderer would be. In particular, the `HostConfig` object mentioned above is never explicitly declared, and instead is a *module* in our code. However, its exports correspond directly to properties on a `HostConfig` object you'd need to declare in your code:\n\n* [React ART](https://github.com/facebook/react/blob/main/packages/react-art/src/ReactART.js) and its [host config](https://github.com/facebook/react/blob/main/packages/react-art/src/ReactARTHostConfig.js)\n* [React DOM](https://github.com/facebook/react/blob/main/packages/react-dom/src/client/ReactDOM.js) and its [host config](https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/client/ReactDOMHostConfig.js)\n* [React Native](https://github.com/facebook/react/blob/main/packages/react-native-renderer/src/ReactNativeRenderer.js) and its [host config](https://github.com/facebook/react/blob/main/packages/react-native-renderer/src/ReactNativeHostConfig.js)\n\nIf these links break please file an issue and we’ll fix them. They intentionally link to the latest versions since the API is still evolving. If you have more questions please file an issue and we’ll try to help!\n\n## An (Incomplete!) Reference\n\nAt the moment, we can't commit to documenting every API detail because the host config still changes very often between the releases. The documentation below is **provided in the spirit of making our best effort rather than an API guarantee**. It focuses on the parts that don't change too often. This is a compromise that strikes a balance between the need for a fast-paced development of React itself, and the usefulness of this package to the custom renderer community. If you notice parts that are out of date or don't match how the latest stable update is behaving, please file an issue or send a pull request, although a response might take time.\n\n#### Modes\n\nThe reconciler has two modes: mutation mode and persistent mode. You must specify one of them.\n\nIf your target platform is similar to the DOM and has methods similar to `appendChild`, `removeChild`, and so on, you'll want to use the **mutation mode**. This is the same mode used by React DOM, React ART, and the classic React Native renderer.\n\n```js\nconst HostConfig = {\n  // ...\n  supportsMutation: true,\n  // ...\n}\n```\n\nIf your target platform has immutable trees, you'll want the **persistent mode** instead. In that mode, existing nodes are never mutated, and instead every change clones the parent tree and then replaces the whole parent tree at the root. This is the mode used by the new React Native renderer, codenamed \"Fabric\".\n\n```js\nconst HostConfig = {\n  // ...\n  supportsPersistence: true,\n  // ...\n}\n```\n\nDepending on the mode, the reconciler will call different methods on your host config.\n\nIf you're not sure which one you want, you likely need the mutation mode.\n\n#### Core Methods\n\n#### `createInstance(type, props, rootContainer, hostContext, internalHandle)`\n\nThis method should return a newly created node. For example, the DOM renderer would call `document.createElement(type)` here and then set the properties from `props`.\n\nYou can use `rootContainer` to access the root container associated with that tree. For example, in the DOM renderer, this is useful to get the correct `document` reference that the root belongs to.\n\nThe `hostContext` parameter lets you keep track of some information about your current place in the tree. To learn more about it, see `getChildHostContext` below.\n\nThe `internalHandle` data structure is meant to be opaque. If you bend the rules and rely on its internal fields, be aware that it may change significantly between versions. You're taking on additional maintenance risk by reading from it, and giving up all guarantees if you write something to it.\n\nThis method happens **in the render phase**. It can (and usually should) mutate the node it has just created before returning it, but it must not modify any other nodes. It must not register any event handlers on the parent tree. This is because an instance being created doesn't guarantee it would be placed in the tree — it could be left unused and later collected by GC. If you need to do something when an instance is definitely in the tree, look at `commitMount` instead.\n\n#### `createTextInstance(text, rootContainer, hostContext, internalHandle)`\n\nSame as `createInstance`, but for text nodes. If your renderer doesn't support text nodes, you can throw here.\n\n#### `appendInitialChild(parentInstance, child)`\n\nThis method should mutate the `parentInstance` and add the child to its list of children. For example, in the DOM this would translate to a `parentInstance.appendChild(child)` call.\n\nThis method happens **in the render phase**. It can mutate `parentInstance` and `child`, but it must not modify any other nodes. It's called while the tree is still being built up and not connected to the actual tree on the screen.\n\n#### `finalizeInitialChildren(instance, type, props, rootContainer, hostContext)`\n\nIn this method, you can perform some final mutations on the `instance`. Unlike with `createInstance`, by the time `finalizeInitialChildren` is called, all the initial children have already been added to the `instance`, but the instance itself has not yet been connected to the tree on the screen.\n\nThis method happens **in the render phase**. It can mutate `instance`, but it must not modify any other nodes. It's called while the tree is still being built up and not connected to the actual tree on the screen.\n\nThere is a second purpose to this method. It lets you specify whether there is some work that needs to happen when the node is connected to the tree on the screen. If you return `true`, the instance will receive a `commitMount` call later. See its documentation below.\n\nIf you don't want to do anything here, you should return `false`.\n\n#### `prepareUpdate(instance, type, oldProps, newProps, rootContainer, hostContext)`\n\nReact calls this method so that you can compare the previous and the next props, and decide whether you need to update the underlying instance or not. If you don't need to update it, return `null`. If you need to update it, you can return an arbitrary object representing the changes that need to happen. Then in `commitUpdate` you would need to apply those changes to the instance.\n\nThis method happens **in the render phase**. It should only *calculate* the update — but not apply it! For example, the DOM renderer returns an array that looks like `[prop1, value1, prop2, value2, ...]` for all props that have actually changed. And only in `commitUpdate` it applies those changes. You should calculate as much as you can in `prepareUpdate` so that `commitUpdate` can be very fast and straightforward.\n\nSee the meaning of `rootContainer` and `hostContext` in the `createInstance` documentation.\n\n#### `shouldSetTextContent(type, props)`\n\nSome target platforms support setting an instance's text content without manually creating a text node. For example, in the DOM, you can set `node.textContent` instead of creating a text node and appending it.\n\nIf you return `true` from this method, React will assume that this node's children are text, and will not create nodes for them. It will instead rely on you to have filled that text during `createInstance`. This is a performance optimization. For example, the DOM renderer returns `true` only if `type` is a known text-only parent (like `'textarea'`) or if `props.children` has a `'string'` type. If you return `true`, you will need to implement `resetTextContent` too.\n\nIf you don't want to do anything here, you should return `false`.\n\nThis method happens **in the render phase**. Do not mutate the tree from it.\n\n#### `getRootHostContext(rootContainer)`\n\nThis method lets you return the initial host context from the root of the tree. See `getChildHostContext` for the explanation of host context.\n\nIf you don't intend to use host context, you can return `null`.\n\nThis method happens **in the render phase**. Do not mutate the tree from it.\n\n#### `getChildHostContext(parentHostContext, type, rootContainer)`\n\nHost context lets you track some information about where you are in the tree so that it's available inside `createInstance` as the `hostContext` parameter. For example, the DOM renderer uses it to track whether it's inside an HTML or an SVG tree, because `createInstance` implementation needs to be different for them.\n\nIf the node of this `type` does not influence the context you want to pass down, you can return `parentHostContext`. Alternatively, you can return any custom object representing the information you want to pass down.\n\nIf you don't want to do anything here, return `parentHostContext`.\n\nThis method happens **in the render phase**. Do not mutate the tree from it.\n\n#### `getPublicInstance(instance)`\n\nDetermines what object gets exposed as a ref. You'll likely want to return the `instance` itself. But in some cases it might make sense to only expose some part of it.\n\nIf you don't want to do anything here, return `instance`.\n\n#### `prepareForCommit(containerInfo)`\n\nThis method lets you store some information before React starts making changes to the tree on the screen. For example, the DOM renderer stores the current text selection so that it can later restore it. This method is mirrored by `resetAfterCommit`.\n\nEven if you don't want to do anything here, you need to return `null` from it.\n\n#### `resetAfterCommit(containerInfo)`\n\nThis method is called right after React has performed the tree mutations. You can use it to restore something you've stored in `prepareForCommit` — for example, text selection.\n\nYou can leave it empty.\n\n#### `preparePortalMount(containerInfo)`\n\nThis method is called for a container that's used as a portal target. Usually you can leave it empty.\n\n#### `scheduleTimeout(fn, delay)`\n\nYou can proxy this to `setTimeout` or its equivalent in your environment.\n\n#### `cancelTimeout(id)`\n\nYou can proxy this to `clearTimeout` or its equivalent in your environment.\n\n#### `noTimeout`\n\nThis is a property (not a function) that should be set to something that can never be a valid timeout ID. For example, you can set it to `-1`.\n\n#### `supportsMicrotasks`\n\nSet this to true to indicate that your renderer supports `scheduleMicrotask`. We use microtasks as part of our discrete event implementation in React DOM. If you're not sure if your renderer should support this, you probably should. The option to not implement `scheduleMicrotask` exists so that platforms with more control over user events, like React Native, can choose to use a different mechanism.\n#### `scheduleMicrotask(fn)`\n\nOptional. You can proxy this to `queueMicrotask` or its equivalent in your environment.\n\n#### `isPrimaryRenderer`\n\nThis is a property (not a function) that should be set to `true` if your renderer is the main one on the page. For example, if you're writing a renderer for the Terminal, it makes sense to set it to `true`, but if your renderer is used *on top of* React DOM or some other existing renderer, set it to `false`.\n\n#### `getCurrentEventPriority`\n\nTo implement this method, you'll need some constants available on the special `react-reconciler/constants` entry point:\n\n```js\nimport {\n  DiscreteEventPriority,\n  ContinuousEventPriority,\n  DefaultEventPriority,\n} from 'react-reconciler/constants';\n\nconst HostConfig = {\n  // ...\n  getCurrentEventPriority() {\n    return DefaultEventPriority;\n  },\n  // ...\n}\n\nconst MyRenderer = Reconciler(HostConfig);\n```\n\nThe constant you return depends on which event, if any, is being handled right now. (In the browser, you can check this using `window.event && window.event.type`).\n\n* **Discrete events:** If the active event is _directly caused by the user_ (such as mouse and keyboard events) and _each event in a sequence is intentional_ (e.g. `click`), return `DiscreteEventPriority`. This tells React that they should interrupt any background work and cannot be batched across time.\n\n* **Continuous events:** If the active event is _directly caused by the user_ but _the user can't distinguish between individual events in a sequence_ (e.g. `mouseover`), return `ContinuousEventPriority`. This tells React they should interrupt any background work but can be batched across time.\n\n* **Other events / No active event:** In all other cases, return `DefaultEventPriority`. This tells React that this event is considered background work, and interactive events will be prioritized over it.\n\nYou can consult the `getCurrentEventPriority()` implementation in `ReactDOMHostConfig.js` for a reference implementation.\n\n### Mutation Methods\n\nIf you're using React in mutation mode (you probably do), you'll need to implement a few more methods.\n\n#### `appendChild(parentInstance, child)`\n\nThis method should mutate the `parentInstance` and add the child to its list of children. For example, in the DOM this would translate to a `parentInstance.appendChild(child)` call.\n\nAlthough this method currently runs in the commit phase, you still should not mutate any other nodes in it. If you need to do some additional work when a node is definitely connected to the visible tree, look at `commitMount`.\n\n#### `appendChildToContainer(container, child)`\n\nSame as `appendChild`, but for when a node is attached to the root container. This is useful if attaching to the root has a slightly different implementation, or if the root container nodes are of a different type than the rest of the tree.\n\n#### `insertBefore(parentInstance, child, beforeChild)`\n\nThis method should mutate the `parentInstance` and place the `child` before `beforeChild` in the list of its children. For example, in the DOM this would translate to a `parentInstance.insertBefore(child, beforeChild)` call.\n\nNote that React uses this method both for insertions and for reordering nodes. Similar to DO","readmeBorrowedFrom":{"version":"17.0.4","source":"wally"},"readmeTruncated":true,"readmeFull":"https://api.forest.dev/v1/package/jsdotlua/roblox/react-reconciler/17.2.1/readme"}