Skip to content

Instantly share code, notes, and snippets.

@alexeyraspopov
Last active May 22, 2020 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeyraspopov/ca70310fc1ea2de34311a4cc9d3a5028 to your computer and use it in GitHub Desktop.
Save alexeyraspopov/ca70310fc1ea2de34311a4cc9d3a5028 to your computer and use it in GitHub Desktop.

Представим компонент, написанный какое-то время назад, используется в приложении, работает без проблем

function MyComponent({ input }) {
  let [something, setSomething] = useState();
  let data = useSomeFetchLibrary(input);
  return (
    <section>
      <ContentA input={something} />
      <ContentB input={data} />
    </section>
  );
}

Нам нужно его расширить, добавив данные которые используют библиотеку N. Сколько кода добавится, изменится, удалится?

Recoil

Данные которые нужно использовать в компоненте:

let userProfile = atom({
  key: "userProfile",
  default: { name: "Anon" },
});

Изменения:

function MyComponent({ input }) {
  let [something, setSomething] = useState();
  let data = useSomeFetchLibrary(input);
+ let profile = useRecoilValue(userProfile);
  return (
    <section>
      <ContentA input={something} />
      <ContentB input={data} />
+     <ContentC profile={profile.name} />
    </section>
  );
}

MobX

Данные которые нужно использовать в компоненте:

let userProfile = observable({ name: "Anon" });

Изменения:

function MyComponent({ input }) {
  let [something, setSomething] = useState();
  let data = useSomeFetchLibrary(input);
+ let profile = userProfile;
-  return (
+  return useObserver(() => (
    <section>
      <ContentA input={something} />
      <ContentB input={data} />
+     <ContentC profile={profile.name} />
    </section>
-  );
+  ));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment