Application Emoji search ReactJS – Recoil

Tram Ho

Introduce

First, the emoji search application is one of the basic examples of reactjs, you can see here https://github.com/ahfarmer/emoji-search

This article I have rewritten this application, along with applying Recoiljs in state management

Emoji Search application

First, to manage the state with Recoil, I will have to create atoms

src / Atoms / searchInput.js

src / Atoms / emojiList.js

This app I just need 2 basic atoms, searchInput used when searching emoji, emojiList to save data. For the emojiList I want to use the atomFamily function, but for some reason I can’t use it

Once you have the atom, you can use the selector to “process” the data you need.

src / Selectors / filteredEmojiList.js

src / common.js

Now I just need to render the data to the UI again.

src / App.js

RecoilRoot will create a context so we can use other functions of Recoil . You should put it outside the App. React.Suspense is a feature of React still under development, but when you use Recoil, it will ask you to use this feature, when you fetch data can display the component instead.

Below is the Emoji component, which is easy to follow, so I wrote other components in the same file.

src / Components / Emoji.js

You can see the hooks of Recoil: useRecoilValue and useRecoilState . These are all basic hooks, you can refer to here.

https://recoiljs.org/docs/api-reference/core/useRecoilState

Result

You can see the demo and the complete code of the application here: https://codesandbox.io/s/admiring-albattani-wm1f9

I quite like this library because it is easy to share the state, the syntax is also very concise and easy to understand. Maybe I will use it from now on instead of using React Context.

Share the news now

Source : Viblo