I am trying to create a UI library for (P)React. Repo can be viewed here.
A setup function is called to initiate createElement/forwardRef functions. These are set to let variables so they can be accessed inside other files. This question is related to this and this. Variables are live views, but why are variables with functions not live views?
// setup.ts
let forwardRef
let createElement
function setup(createElementIn, { forwardRef : fw }) {
forwardRef = fw
createElement = createElementIn
}
In another function it is imported like this
// box.ts
import { forwardRef, createElement } from './setup'
// this works
function Box(props, ref){
console.log('forwardRef', forwardRef) // correct function
return createElement('div', { ...props, ...(ref && ref) })
}
// this does not work because forwardRef is undefined
export forwardRef ? forwardRef(Box) : Box
Box is called like this
import { useRef, createElement, forwardRef } from 'react'
import { Box, setup } from 'juhuui'
// this is called only once
setup(createElement, { forwardRef })
function Hello () {
const myRef = useRef(null);
return (<Box ref={myRef}>Hello</Box>)
}
Can you please tell me how my code can be restructured to work?