KevinAst / feature-u

Feature Based Project Organization for React
https://feature-u.js.org/
MIT License
91 stars 6 forks source link

Note on initialization #32

Open ScreamZ opened 4 years ago

ScreamZ commented 4 years ago

Hey,

I've been using NextJS, but also the last version of Expo that doesn't directly expose component registration. Sometimes you want full control and don't care about "DOM-like" registration.

I ended up with such pattern :

import React from "react";
import { SafeAreaView, Text } from "react-native";
import { launchApp } from "feature-u";
import features from "./features";

export default function App() {
  const [RootElement, setRootElement] = React.useState();
  const [message, setMessage] = React.useState("");

  React.useEffect(() => {
    launchApp({
      features,
      registerRootAppElm(rootAppElm) {
        setRootElement(rootAppElm);
      },
      showStatus(message, error) {
        setMessage(message ?? error.message);
      },
    });
  }, []);

  if (!message && typeof RootElement !== "undefined") {
    return RootElement;
  }

  return (
    <SafeAreaView>
      <Text>{message}</Text>
    </SafeAreaView>
  );
}

While kind of hacky, this works fine, it might help some people. I don't like the way to block UI rendering while everything is loading. I might want to hook some "Homemade" splash screen on a mobile app.

Hope this help :) Andréas

PS: I'm also working on a typescript definition file for the library, I'll make a PR soon

KevinAst commented 4 years ago

Thanks Andréas. I haven't been following Expo releases, and I am not a NextJS user.

Evidently as of SDK 18, Expo's registerRootComponent() is handled automatically. With that said, my understanding is that you can continue to use it if you prefer. I wonder if it is in danger of being fully deprecated?

Question: Can you highlight the advantage of not using Expo's registerRootComponent()?

At any rate, thanks for the example! I will keep this post open for better visibility.