# Video: Haxe and React using create-react-app workflow

**URL:** <https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192>\
**Category:** Haxe\
**Created:** [August 19, 2021, 10:23pm UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192 "2021-08-19T22:23:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![cambiata](https://community.haxe.org/user_avatar/community.haxe.org/cambiata/32/61_2.png) [@cambiata](https://community.haxe.org/u/cambiata)\
**Post date:** [August 19, 2021, 10:23pm UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/1 "2021-08-19T22:23:42Z")

</div>

Hi!

Just published a video introduction on how to integrate Haxe into a React create-react-app workflow. Includes using benmerckx’s [watch](https://github.com/benmerckx/watch) library for auto-compiling the Haxe code, and using haxiomic’s [dts2hx](https://github.com/haxiomic/dts2hx) library for generating Haxe externs from third-party react libraries. Brief introductions on how to use functional components including hooks, as well as class-based components - all in a typesafe manner.

[![](https://community.haxe.org/uploads/default/original/2X/6/6a0f3b8ff8d0f2dd0d39184ba0afa54b2578ed6b.jpeg "Haxe and React using create-react-app workflow") ](https://www.youtube.com/watch?v=sUlIgZ1hHR4)

/ Jonas

---

<div class="post-metadata">

**Author:** ![kLabz](https://community.haxe.org/user_avatar/community.haxe.org/klabz/32/225_2.png) [@kLabz](https://community.haxe.org/u/kLabz)\
**Post date:** [August 20, 2021, 5:51am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/2 "2021-08-20T05:51:08Z")

</div>

Hey, nice! Definitely easier to get started with haxe + react this way 🙂

Couple notes:

You might want to check `React.createRef()` for easier refs (and don’t ignore `null` values sent to your ref callback, it usually means your component unmounted, and you don’t want to keep an outdated reference).

Also, careful with “type safety” here; you don’t really have it in several places (hooks, context, and even props on the jsx side).

---

<div class="post-metadata">

**Author:** ![back2dos](https://community.haxe.org/user_avatar/community.haxe.org/back2dos/32/30_2.png) [@back2dos](https://community.haxe.org/u/back2dos)\
**Post date:** [August 20, 2021, 10:27am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/3 "2021-08-20T10:27:05Z")

</div>

> [@kLabz](#):
>
> Also, careful with “type safety” here; you don’t really have it in several places (hooks, context, and even props on the jsx side).

It’s probably worth mentioning react-next for increased type safety in jsx (although I can’t fully comment on how far it goes these days).

As for hooks, I don’t see why they would not be typed. It is however true that the dependencies array is not statically checked, as it is with eslint/tslint in create-react-app’s setup. From my own experience, that’s not necessarily a down side though 😃

---

<div class="post-metadata">

**Author:** ![kLabz](https://community.haxe.org/user_avatar/community.haxe.org/klabz/32/225_2.png) [@kLabz](https://community.haxe.org/u/kLabz)\
**Post date:** [August 20, 2021, 10:35am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/4 "2021-08-20T10:35:56Z")

</div>

> It’s probably worth mentioning react-next for increased type safety in jsx (although I can’t fully comment on how far it goes these days).

Yeah… but I don’t think it would work as well with dts2hx because of that =/

> As for hooks, I don’t see why they would not be typed.

Yeah it depends how you write them, I double checked the video and it was fine I suppose

---

<div class="post-metadata">

**Author:** ![araneta](https://community.haxe.org/user_avatar/community.haxe.org/araneta/32/1737_2.png) [@araneta](https://community.haxe.org/u/araneta)\
**Post date:** [October 8, 2021, 7:03am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/5 "2021-10-08T07:03:41Z")

</div>

thanks for this great tutorial but i have some problem using react.ReactComponent here are the error messages:  
workspaces/shirase/apps/testreact/src-hx/import.hx:4: characters 8-28 : Type not found : react.ReactComponent  
/workspaces/shirase/apps/testreact/src-hx/import.hx:5: characters 8-24 : Type not found : react.ReactMacro  
/workspaces/shirase/apps/testreact/src-hx/import.hx:9: characters 8-23 : Type not found : szhin.ReactMenu  
/workspaces/shirase/apps/testreact/src-hx/import.hx:10: characters 8-23 : Type not found : szhin.ReactMenu  
/workspaces/shirase/apps/testreact/src-hx/import.hx:11: characters 8-23 : Type not found : szhin.ReactMenu  
src-hx/Index.hx:4: characters 8-28 : Type not found : react.ReactComponent  
src-hx/Index.hx:5: characters 8-24 : Type not found : react.ReactMacro  
/workspaces/shirase/apps/testreact/src-hx/import.hx:4: characters 8-28 : Type not found : react.ReactComponent  
/workspaces/shirase/apps/testreact/src-hx/import.hx:5: characters 8-24 : Type not found : react.ReactMacro  
/workspaces/shirase/apps/testreact/src-hx/import.hx:9: characters 8-23 : Type not found : szhin.ReactMenu  
/workspaces/shirase/apps/testreact/src-hx/import.hx:10: characters 8-23 : Type not found : szhin.ReactMenu  
/workspaces/shirase/apps/testreact/src-hx/import.hx:11: characters 8-23 : Type not found : szhin.ReactMenu  
src-hx/react/ReactHooks.hx:5: characters 8-19 : Type not found : react.React  
src-hx/react/ReactHooks.hx:13: characters 40-55 : Type not found : ReactContext

here is my code [https://github.com/araneta/shirase/tree/main/apps/testreact](https://github.com/araneta/shirase/tree/main/apps/testreact)  
how to fix this?  
Thanks

---

<div class="post-metadata">

**Author:** ![cambiata](https://community.haxe.org/user_avatar/community.haxe.org/cambiata/32/61_2.png) [@cambiata](https://community.haxe.org/u/cambiata)\
**Post date:** [October 8, 2021, 6:03pm UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/6 "2021-10-08T18:03:34Z")

</div>

Hi araneta!

It seems that something in the dts2hx process has changed, probably to a new version of @szhsin-react-menu, so that dts2hx now creates completel new react externs. Have a look into .haxlib/react/ folder, there’s a 17.0.27 foldler, probably created by dts2hx. That’s what’s causing the `Type not found: react.ReactComponent` problem, if I’m not mistaken…

You can switch back to the original haxelib version by changing the content of the text file `.haxelib/react/.current` to `1.12.0`.

So that solves half of the problem - the szhsin externs won’t work now, because they are refering to the newer `17.0.27` version…

Creating externs manually for React components actually isn’t that hard. Here’s a quick example:

```haxe
// ReactMenu.hx
// Drop this file into your your haxe project src folder

@:jsRequire('@szhsin/react-menu')
extern class ReactMenu {
	static function useMenuState(?options:ReactMenuStateOptions):{
		@:optional
		var state:ReactMenuState;
		dynamic function toggleMenu(?open:Bool):Void;
		dynamic function endTransition():Void;
	};

	static final MenuButton:Dynamic;
	static final Menu:Dynamic;
	static final ControlledMenu:Dynamic;
	static final SubMenu:Dynamic;
	static final MenuItem:Dynamic;
	static final FocusableItem:Dynamic;
	static final MenuDivider:Dynamic;
	static final MenuHeader:Dynamic;
	static final MenuGroup:Dynamic;
	static final MenuRadioGroup:Dynamic;
	// static function applyHOC<H>(hoc:H):H;
	// static function applyStatics<W>(sourceComponent:Dynamic):(w:W) -> W;
}

typedef ReactMenuStateOptions = {
	@:optional
	var initialMounted:Bool;
	@:optional
	var unmountOnClose:Bool;
	@:optional
	var transition:Dynamic;
	@:optional
	var transitionTimeout:Float;
};

typedef ReactMenuState = String;

```

Then you can use it like this:

```haxe
import react.ReactMacro.jsx;
import ReactMenu.MenuItem;
import ReactMenu.MenuButton;
import ReactMenu.Menu;
import js.Browser.alert;

function TestMenuHx() {
js.Lib.require('@szhsin/react-menu/dist/index.css');
var menuButton = jsx('<MenuButton>ClickMe</MenuButton>');
return jsx('<div>    
	<h1>TestMenuHx</h1>
	<Menu menuButton=${menuButton}>
		<MenuItem>New File</MenuItem>
		<MenuItem onClick=${e -> alert('Hello')}>Save</MenuItem>
	</Menu>        
	</div>');
}

```

Note that you then don’t need the `-L szhsin-react-menu` flag in your `build.hxml` file.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![araneta](https://community.haxe.org/user_avatar/community.haxe.org/araneta/32/1737_2.png) [@araneta](https://community.haxe.org/u/araneta)\
**Post date:** [October 20, 2021, 1:19am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/7 "2021-10-20T01:19:44Z")

</div>

I tried to compile this code in other computer but i got this error:  
$haxe build.hxml  
the error is  
src-hx/Index.hx:10: characters 1-9 : Unexpected function  
what should i do?

the content of Index.hx is

```haxe
import js.Browser.document;

import react.ReactDOM;
import react.ReactComponent;
import react.ReactMacro.jsx;
import react.ReactHooks;

//import react_loadable.Loadable;

function main(){
    trace('elodas');
    ReactDOM.render(jsx('<div><h1>dasdas</h1> <Greeting greeter=
    "Luci" />
    <ClickCounter />
    <TestComponent name="bob" age=${33} />
    </div>'), document.getElementById('root'));
}

function Greeting(props: {greeter: String}) return jsx('<h2>Hello, ${props.greeter}!</h2>');

function ClickCounter(){
	var counts = ReactHooks.useState(0);
	return jsx('<button onClick=${e->counts.value = counts.value +1}>${counts.value} counts</button>');
}

```

---

<div class="post-metadata">

**Author:** ![kLabz](https://community.haxe.org/user_avatar/community.haxe.org/klabz/32/225_2.png) [@kLabz](https://community.haxe.org/u/kLabz)\
**Post date:** [October 20, 2021, 4:53am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/8 "2021-10-20T04:53:48Z")

</div>

What version of Haxe are you using?

---

<div class="post-metadata">

**Author:** ![araneta](https://community.haxe.org/user_avatar/community.haxe.org/araneta/32/1737_2.png) [@araneta](https://community.haxe.org/u/araneta)\
**Post date:** [October 26, 2021, 6:29am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/9 "2021-10-26T06:29:53Z")

</div>

version 4.0.5  
haxe --version  
4.0.5

---

<div class="post-metadata">

**Author:** ![kLabz](https://community.haxe.org/user_avatar/community.haxe.org/klabz/32/225_2.png) [@kLabz](https://community.haxe.org/u/kLabz)\
**Post date:** [October 26, 2021, 7:00am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/10 "2021-10-26T07:00:19Z")

</div>

You need at least 4.2.0 to use module level functions. Before that version you need a class around your functions, which in this example will also need to be declared as static.

---

<div class="post-metadata">

**Author:** ![araneta](https://community.haxe.org/user_avatar/community.haxe.org/araneta/32/1737_2.png) [@araneta](https://community.haxe.org/u/araneta)\
**Post date:** [October 26, 2021, 7:08am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/11 "2021-10-26T07:08:36Z")

</div>

> [@kLabz](#):
>
> 4.2.0

how to upgrade the haxe version, i’m using linux mint 20. Thanks for your help

---

<div class="post-metadata">

**Author:** ![tokiop](https://avatars.discourse-cdn.com/v2/letter/t/bc8723/32.png) [@tokiop](https://community.haxe.org/u/tokiop)\
**Post date:** [October 30, 2021, 9:56am UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/12 "2021-10-30T09:56:48Z")

</div>

You can have a look at [lix-pm](https://github.com/lix-pm/lix.client), which helps to install and version your project dependencies : haxe version, libraries (from haxelib, github…). It allows your project to be portable across computers and through time because it locks the libs’ and haxe versions your required, and install them if needed.

Part of it is an “haxe installer”, so you can use the version you want for your project, without manually installing it globally or relying on your system’s packaged version. The readme describes well how to get started.

---

<div class="post-metadata">

**Author:** ![araneta](https://community.haxe.org/user_avatar/community.haxe.org/araneta/32/1737_2.png) [@araneta](https://community.haxe.org/u/araneta)\
**Post date:** [October 31, 2021, 2:33pm UTC](https://community.haxe.org/t/video-haxe-and-react-using-create-react-app-workflow/3192/13 "2021-10-31T14:33:13Z")

</div>

thank you so much i will try it
