CodeBlanks Logo Icon CodeBlanks

Challenge: Custom renderer skeleton

Write a skeleton with react-reconciler that prints to the console.

Challenge: Custom renderer skeleton

In this challenge you will write a host config skeleton for a custom renderer. You will define createInstance and appendChild functions.

This skeleton provides the basis for rendering React to a non-DOM target. The host config can be written for terminals, canvas or mobile native environments. The React core logic stays the same.

Practice Write the customHostConfig

Write a customHostConfig. Set supportsMutation: true. Add createInstance: (type) => ({ type, children: [] }) and appendChild: (parent, child) => { parent.children.push(child); return parent; }. Export it.

Starter code

App.jsx
// Write the customHostConfig here

Sample solution

App.jsx
const customHostConfig = {
  supportsMutation: true,
  createInstance: (type) => ({ type, children: [] }),
  appendChild: (parent, child) => {
    parent.children.push(child);
    return parent;
  },
};
export default customHostConfig;

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the role of the host config?

  • Provide target environment functions Correct answer
  • Compile CSS
  • Send HTTP requests
  • Generate TypeScript types

Explanation: The host config provides target-specific functions.

2 Fill in the blank: create a node

Question
const hostConfig = {
  ____: (type) => ({ type, children: [] }),
  appendChild: (parent, child) => parent.children.push(child),
};
Answer
const hostConfig = {
  createInstance: (type) => ({ type, children: [] }),
  appendChild: (parent, child) => parent.children.push(child),
};

Explanation: createInstance creates a new node.

All lessons in Expert