As showed in typedraft-in-5min, if we declare a function and use it as standalone JSX element, the JSX element will be replaced by the body of this function:
demo.tsx
exportfunctionMain(){
console.log("hello from main");
<ContextA/>;
}
functionContextA(){
console.log("hello from context A");
}
Beyond that, it supports nesting:
demo.tsx
exportfunctionMain(){
console.log("hello from main");
<ContextA/>;
}
functionContextA(){
console.log("hello from context A");
<ContextA2/>;
}
functionContextA2(){
console.log("hello from context A, nested");
}
the result will be:
demo.ts
exportfunctionMain(){
console.log("hello from main");
console.log("hello from context A");
console.log("hello from context A, nested");
}
Sometimes it's not necessary to extract statements as functions, this kind of "statement replacement" simplifies code and helps you to always keep focus on a short snippet of code.
The function declaration will be referred to as macro definition, and the function name will be used as the name of macro. For example, we have macro ContextA and ContextA2.