createFactory lets you create a function that creates a React element of a given type. The type argument can be either a tag name string (such as div or span), a React component type (a class or a function), or a React fragment type.

createFactory is typically invoked if you are using React without JSX.

React.createFactory(type)

Note

createFactory is considered legacy, and we encourage you to either use JSX or use React.createElement() directly instead.

Usage

Creating React elements

In this example, we can render a React element of the type button.

import React from 'react';
import { render } from 'react-dom';

const MyButton = (reactElement = 'button') =>
  (React.createFactory(reactElement))({
    onClick: (evt) => {
      evt.preventDefault();
      alert("I was created by createFactory()");
    }
  }, 'Click');

export default function App(){
    return (
        <div>
            {MyButton()}
        </div>
    )
};

createFactory(type)

Call createFactory(type) to create a function that creates a React element of a given type.

const myElement= React.createFactory(type)

Parameters

  • type: The type argument can be either a tag name string (such as div or span), a React component type (a class or a function), or a React fragment type.

Returns

Returns a function that can be used to create a React element of the given type.