I was writing a Dialog component and this thought came into my mind suddenly.
export const alert = (content: string) => {
const buttons = [<button onClick={()=>closeModal()}>ok</button>] // quite ok
// const buttons = [<button onClick={closeModal}>ok</button>] // raise an error
const closeModal = modal(content, buttons)
}
The error is : Block-scoped variable 'closeModal' used before its declaration. I am so used to wrap some expression in a function in React and never thought about it.
The situation may be simplified to below:
const caller = () => {
func() // ok
}
const funcAgain = func // error
func() // error
const func = () => {}
What's this behavior called? Does it have something to do with closure? or variable hoisting?