Migrating from React
React is an excellent UI building library, and its ecosystem has many outstanding tools that Gem draws inspiration from, with the aim of creating a native, compile-free, easy-to-use WebApp development framework.
Components to Custom Elements
First, let's look at a simple React component. The function name here can be used as a tag name in other components, and the component's properties are marked with IProps, which ultimately returns the rendered content of the component:
In Gem, custom elements are defined using Classes, and elements must be registered with @customElement, allowing them to be used in templates. Class fields are used to define element properties, for example, using decorators like @attribute to make properties reactive, which gives the fields a similar role to the properties in IProps of the above React component. Finally, a render method is defined, which ultimately returns the element's content template. This is equivalent to what a React component returns, except that the template uses JavaScript template literals instead of JSX. This is why Gem does not require compilation and can run directly in Vanilla JavaScript.
When developing components, it's often necessary to maintain internal state and obtain references to child components. In React, this is done using useState and useRef:
In Gem, you use createState and createRef:
Generally, React components are not that simple; they may have side effects or require memoization for some recalculations. These needs are handled in React using Hooks, for example, memoizing the serialization result from the above React component and logging it after mounting:
In Gem, this is accomplished by decorating functions with decorators:
Overall, custom components written in Gem are more complex than React components, but the benefit is that they can be used in Vanilla JavaScript.
CSS Modules to Gem
Writing a component cannot avoid styles. In React, to make styles modular, developers usually use CSS Modules or CSS-in-JS solutions like Styled Components. Here's how to use CSS Modules in a React component:
In Gem, you need to manually create an object and apply it to the elements (should auto adopt?):
Other tools to Gem
- React Router =>
<gem-light-route> - React Redux =>
Gem.createStore - Theming =>
@mantou/gem/helper/theme - I18n =>
@mantou/gem/helper/i18n - Request =>
@mantou/gem/helper/request