Як створити простий веб-додаток для покемонів за допомогою гачків React та контекстного API

Після семи років повної розробки стеків із використанням Ruby, Python та vanilla JavaScript, сьогодні я переважно працюю з JavaScript, Typescript, React та Redux.

Спільнота JavaScript чудова і рухається дуже швидко. Тонни речей створюються "за одну ніч", як правило, в переносному значенні, але іноді буквально. Все це ускладнює те, щоб бути в курсі подій.

Я завжди відчуваю, що запізнююсь на вечірку JavaScript. І я хочу бути там, хоча я не дуже люблю вечірки.

Всього один рік роботи з React та Redux, і я відчув, що мені потрібно навчитися новим речам, таким як Hooks та Context API для управління станом. Прочитавши кілька статей про це, я захотів спробувати ці концепції, тому створив простий проект як лабораторію, щоб експериментувати з цими речами.

З маленького хлопчика я захоплювався покемонами. Завжди було весело грати в ігри на Game Boy і підкорювати всі Ліги. Тепер, як розробник, я хочу пограти з Pokémon API.

Я вирішив створити просту веб-сторінку, де я міг би ділитися даними між різними частинами сторінки. Сторінка мала три основні розділи:

  • Поле зі списком усіх існуючих покемонів
  • Поле зі списком усіх захоплених покемонів
  • Поле із введенням, щоб додати новий покемон до списку

І кожне поле матиме таку поведінку або дії:

  • Для кожного покемону в першій коробці я можу захопити їх і відправити у другу коробку
  • Для кожного покемону у другій коробці я можу відпустити їх і надіслати до першої скриньки
  • Як бог гри, я можу створювати покемони, заповнивши введення та надіславши їх до першого вікна

Тож усі функції, які я хотів реалізувати, були зрозумілі - списки та дії.

Перелік покемонів

Основною особливістю, яку я хотів спочатку створити, був перелік покемонів. Отже, для масиву об’єктів я хотів перерахувати та показати nameатрибут кожного об’єкта.

Я розпочав із першої коробки: існуючий покемон.

Спочатку я думав, що мені не потрібен Pokémon API - я міг просто знущатися зі списку і перевірити, чи він працює. За допомогою useStateя можу оголосити стан свого компонента та використовувати його.

Ми визначаємо його зі значенням за замовчуванням для фіктивного списку покемонів, лише щоб перевірити:

const [pokemons] = useState([ { id: 1, name: 'Bulbasaur' }, { id: 2, name: 'Charmander' }, { id: 3, name: 'Squirtle' } ]); 

Тут ми маємо перелік трьох об’єктів покемонів. useStateГачок забезпечує пару елементів: поточний стан та функція , щоб оновити це створене стан.

Тепер, маючи стан покемона, ми можемо скласти його на карту і зробити ім’я кожного з них.

{pokemons.map((pokemon) =>

{pokemon.name}

)}

Це просто карта, що повертає ім’я кожного покемона в тезі абзацу.

Це весь реалізований компонент:

import React, { useState } from 'react'; const PokemonsList = () => { const [pokemons] = useState([ { id: 1, name: 'Bulbasaur' }, { id: 2, name: 'Charmander' }, { id: 3, name: 'Squirtle' } ]); return ( 

Pokemons List

{pokemons.map((pokemon) =>

{pokemon.id}

{pokemon.name}

)} ) } export default PokemonsList;

Тільки трохи налаштуйте тут:

  • Я додав keyу поєднанні покемонів idтаname
  • І я також надав абзац для idатрибута (я просто тестував його. Але ми його видалимо пізніше).

Чудово! Тепер у нас є перший список і працює.

Я хочу зробити це саме, але зараз для захопленого покемону. Але для захопленого покемону я спочатку хочу створити порожній список, тому що коли починається "гра", у мене не буде захоплених покемонів, так? Правильно!

const [pokemons] = useState([]); 

Ось і все, насправді просто!

Весь компонент схожий на інший:

import React, { useState } from 'react'; const CapturedPokemons = () => { const [pokemons] = useState([]); return ( 

Captured Pokemons

{pokemons.map((pokemon) =>

{pokemon.id}

{pokemon.name}

)} ) } export default CapturedPokemons;

Тут ми використовуємо map, але оскільки масив порожній, він нічого не відображає.

Тепер, коли у мене є два основні компоненти, я можу використовувати їх разом у Appкомпоненті:

import React from 'react'; import './App.css'; import PokemonsList from './PokemonsList'; import Pokedex from './Pokedex'; const App = () => ( ); export default App; 

Захоплення та звільнення

Це друга частина нашого додатку, де ми можемо захоплювати та випускати покемони. Тож давайте переглянемо очікувану поведінку.

Для кожного покемону у списку доступних покемонів я хочу активувати дію для їх захоплення. Дія захоплення вилучить їх зі списку, де вони були, і додає до списку захоплених покемонів.

The release action will have similar behavior. But instead of moving from the available list to the captured list, it will be the reverse. We will move them from the captured list to the available list.

So both boxes need to share data to be able to add pokémon to the other list. How do we do this as they are different components in the app? Let's talk about the React Context API.

The Context API was designed to make global data for a defined tree of React components. As the data is global, we can share it among components in this defined tree. So let's use it to share our simple Pokemon data between the two boxes.

Mental Note: "Context is primarily used when some data needs to be accessible by many components at different nesting levels." - React Docs.

Using the API, we simply create a new context like this:

import { createContext } from 'react'; const PokemonContext = createContext(); 

Now, with the PokemonContext, we can use its provider. It will work as a component wrapper of a tree of components. It provides global data to these components and enables them to subscribe to any changes related to this context. It looks like this:

The value prop is just a value that this context provides the wrapped components. What should we provide to the available and the captured lists?

  • pokemons: to list in the available list
  • capturedPokemons: to list in the captured list
  • setPokemons: to be able to update the available list
  • setCapturedPokemons: to be able to update the captured list

As I mentioned before in the useState part, this hook always provides a pair: the state and a function to update this state. This function handles and updates the context state. In other words, they are the setPokemons and setCapturedPokemons. How?

const [pokemons, setPokemons] = useState([ { id: 1, name: 'Bulbasaur' }, { id: 2, name: 'Charmander' }, { id: 3, name: 'Squirtle' } ]); 

Now we have the setPokemons.

const [capturedPokemons, setCapturedPokemons] = useState([]); 

And now we also have the setCapturedPokemons.

With all these values in hand, we can now pass them to the provider's value prop.

import React, { createContext, useState } from 'react'; export const PokemonContext = createContext(); export const PokemonProvider = (props) => { const [pokemons, setPokemons] = useState([ { id: 1, name: 'Bulbasaur' }, { id: 2, name: 'Charmander' }, { id: 3, name: 'Squirtle' } ]); const [capturedPokemons, setCapturedPokemons] = useState([]); const providerValue = { pokemons, setPokemons, capturedPokemons, setCapturedPokemons }; return (  {props.children}  ) }; 

I created a PokemonProvider to wrap all this data and the APIs to create the context and return the context provider with the defined value.

But how do we provide all this data and APIs to the component? We need to do two main things:

  • Wrap the components into this context provider
  • Use the context in each component

Let's wrap them first:

const App = () => ( ); 

And we use the context by using the useContext and passing the created PokemonContext. Like this:

import { useContext } from 'react'; import { PokemonContext } from './PokemonContext'; useContext(PokemonContext); // returns the context provider value we created 

We want to be able to catch the available pokémon, so it would be useful to have the setCapturedPokemons function API update the captured pokémon.

As each pokémon is captured, we need to remove it from the available list. setPokemons is also needed here. And to update each list, we need the current data. So basically we need everything from the context provider.

We need to build a button with an action to capture the pokémon:

  • tag with an onClick calling the capture function and passing the pokémon
+ 
  • The capture function will update the pokemons and the capturedPokemons lists
const capture = (pokemon) => (event) => { // update captured pokemons list // update available pokemons list }; 

To update the capturedPokemons, we can just call the setCapturedPokemons function with the current capturedPokemons and the pokémon to be captured.

setCapturedPokemons([...capturedPokemons, pokemon]); 

And to update the pokemons list, just filter the pokémon that will be captured.

setPokemons(removePokemonFromList(pokemon)); 

removePokemonFromList is just a simple function to filter the pokémon by removing the captured pokémon.

const removePokemonFromList = (removedPokemon) => pokemons.filter((pokemon) => pokemon !== removedPokemon) 

How does the component look now?

import React, { useContext } from 'react'; import { PokemonContext } from './PokemonContext'; export const PokemonsList = () => { const { pokemons, setPokemons, capturedPokemons, setCapturedPokemons } = useContext(PokemonContext); const removePokemonFromList = (removedPokemon) => pokemons.filter(pokemon => pokemon !== removedPokemon); const capture = (pokemon) => () => { setCapturedPokemons([...capturedPokemons, pokemon]); setPokemons(removePokemonFromList(pokemon)); }; return ( 

Pokemons List

{pokemons.map((pokemon) => {pokemon.name} + )} ); }; export default PokemonsList;

It will look very similar to the captured pokémon component. Instead of capture, it will be a release function:

import React, { useContext } from 'react'; import { PokemonContext } from './PokemonContext'; const CapturedPokemons = () => { const { pokemons, setPokemons, capturedPokemons, setCapturedPokemons, } = useContext(PokemonContext); const releasePokemon = (releasedPokemon) => capturedPokemons.filter((pokemon) => pokemon !== releasedPokemon); const release = (pokemon) => () => { setCapturedPokemons(releasePokemon(pokemon)); setPokemons([...pokemons, pokemon]); }; return ( 

CapturedPokemons

{capturedPokemons.map((pokemon) => {pokemon.name} - )} ); }; export default CapturedPokemons;

Reducing complexity

Now we use the useState hook, the Context API, and the context provider useContext. And more importantly, we can share data between pokémon boxes.

Another way to manage the state is by using useReducer as an alternative to useState.

The reducer lifecycle works like this: useReducer provides a dispatch function. With this function, we can dispatch an action inside a component. The reducer receives the action and the state. It understands the type of action, handles the data, and return a new state. Now, the new state can be used in the component.

As an exercise and to have a better understanding of this hook, I tried to replace useState with it.

useState was inside the PokemonProvider. We can redefine the initial state for the available and the captured pokémon in this data structure:

const defaultState = { pokemons: [ { id: 1, name: 'Bulbasaur' }, { id: 2, name: 'Charmander' }, { id: 3, name: 'Squirtle' } ], capturedPokemons: [] }; 

And pass this value to useReducer:

const [state, dispatch] = useReducer(pokemonReducer, defaultState); 

useReducer receives two parameters: the reducer and the initial state. Let's build the pokemonReducer now.

The reducer receives the current state and the action that was dispatched.

const pokemonReducer = (state, action) => // returns the new state based on the action type 

Here we get the action type and return a new state. The action is an object. It looks like this:

{ type: 'AN_ACTION_TYPE' } 

But could also be bigger:

{ type: 'AN_ACTION_TYPE', pokemon: { name: 'Pikachu' } } 

In this case, we'll pass a pokémon to the action object. Let's pause for a minute and think about what we want to do inside the reducer.

Here, we usually update data and handle actions. Actions are dispatched, so actions are behavior. And the behaviors from our app are capture and release! These are the actions we need to handle here.

This is what our reducer will look like:

const pokemonReducer = (state, action) => { switch (action.type) { case 'CAPTURE': // handle capture and return new state case 'RELEASE': // handle release and return new state default: return state; } }; 

If our action type is CAPTURE, we handle it in one way. If our action type is RELEASE, we handle it another way. If the action type doesn't match any of these types, just return the current state.

When we capture the pokémon, we need to update both lists: remove the pokémon from the available list and add it to the captured list. This state is what we need to return from the reducer.

const getPokemonsList = (pokemons, capturedPokemon) => pokemons.filter(pokemon => pokemon !== capturedPokemon) const capturePokemon = (pokemon, state) => ({ pokemons: getPokemonsList(state.pokemons, pokemon), capturedPokemons: [...state.capturedPokemons, pokemon] }); 

The capturePokemon function just returns the updated lists. The getPokemonsList removes the captured pokémon from the available list.

And we use this new function in the reducer:

const pokemonReducer = (state, action) => { switch (action.type) { case 'CAPTURE': return capturePokemon(action.pokemon, state); case 'RELEASE': // handle release and return new state default: return state; } }; 

Now the release function!

const getCapturedPokemons = (capturedPokemons, releasedPokemon) => capturedPokemons.filter(pokemon => pokemon !== releasedPokemon) const releasePokemon = (releasedPokemon, state) => ({ pokemons: [...state.pokemons, releasedPokemon], capturedPokemons: getCapturedPokemons(state.capturedPokemons, releasedPokemon) }); 

The getCapturedPokemons remove the released pokémon from the captured list. The releasePokemon function returns the updated lists.

Our reducer looks like this now:

const pokemonReducer = (state, action) => { switch (action.type) { case 'CAPTURE': return capturePokemon(action.pokemon, state); case 'RELEASE': return releasePokemon(action.pokemon, state); default: return state; } }; 

Just one minor refactor: action types! These are strings and we can extract them into a constant and provide for the dispatcher.

export const CAPTURE = 'CAPTURE'; export const RELEASE = 'RELEASE'; 

And the reducer:

const pokemonReducer = (state, action) => { switch (action.type) { case CAPTURE: return capturePokemon(action.pokemon, state); case RELEASE: return releasePokemon(action.pokemon, state); default: return state; } }; 

The entire reducer file looks like this:

export const CAPTURE = 'CAPTURE'; export const RELEASE = 'RELEASE'; const getCapturedPokemons = (capturedPokemons, releasedPokemon) => capturedPokemons.filter(pokemon => pokemon !== releasedPokemon) const releasePokemon = (releasedPokemon, state) => ({ pokemons: [...state.pokemons, releasedPokemon], capturedPokemons: getCapturedPokemons(state.capturedPokemons, releasedPokemon) }); const getPokemonsList = (pokemons, capturedPokemon) => pokemons.filter(pokemon => pokemon !== capturedPokemon) const capturePokemon = (pokemon, state) => ({ pokemons: getPokemonsList(state.pokemons, pokemon), capturedPokemons: [...state.capturedPokemons, pokemon] }); export const pokemonReducer = (state, action) => { switch (action.type) { case CAPTURE: return capturePokemon(action.pokemon, state); case RELEASE: return releasePokemon(action.pokemon, state); default: return state; } }; 

As the reducer is now implemented, we can import it into our provider and use it in the useReducer hook.

const [state, dispatch] = useReducer(pokemonReducer, defaultState); 

As we are inside the PokemonProvider, we want to provide some value to the consuming components: the capture and release actions.

These functions just need to dispatch the correct action type and pass the pokémon to the reducer.

  • The capture function: it receives the pokémon and returns a new function that dispatches an action with the type CAPTURE and the captured pokémon.
const capture = (pokemon) => () => { dispatch({ type: CAPTURE, pokemon }); }; 
  • The release function: it receives the pokémon and returns a new function that dispatches an action with the type RELEASE and the released pokémon.
const release = (pokemon) => () => { dispatch({ type: RELEASE, pokemon }); }; 

Now with the state and the actions implemented, we can provide these values to the consuming components. Just update the provider value prop.

const { pokemons, capturedPokemons } = state; const providerValue = { pokemons, capturedPokemons, release, capture };  {props.children}  

Great! Now back to the component. Let's use these new actions. All the capture and release logics are encapsulated in our provider and reducer. Our component is pretty clean now. The useContext will look like this:

const { pokemons, capture } = useContext(PokemonContext); 

And the whole component:

import React, { useContext } from 'react'; import { PokemonContext } from './PokemonContext'; const PokemonsList = () => { const { pokemons, capture } = useContext(PokemonContext); return ( 

Pokemons List

{pokemons.map((pokemon) => {pokemon.name} + )} ) }; export default PokemonsList;

For the captured pokémon component, it will look the very similar to the useContext:

const { capturedPokemons, release } = useContext(PokemonContext); 

And the whole component:

import React, { useContext } from 'react'; import { PokemonContext } from './PokemonContext'; const Pokedex = () => { const { capturedPokemons, release } = useContext(PokemonContext); return ( 

Pokedex

{capturedPokemons.map((pokemon) => {pokemon.name} - )} ) }; export default Pokedex;

No logic. Just UI. Very clean.

Pokémon God – The Creator

Now that we have the communication between the two lists, I want to build a third box. This will how we create new pokémon. But it is just a simple input and submit button.

When we add a pokémon's name into the input and press the button, it will dispatch an action to add this pokémon to the available list.

As we need to access the available list to update it, we need to share the state. So our component will be wrapped by our PokemonProvider together with the other components.

const App = () => ( ); 

Let's build the PokemonForm component now. The form is pretty straightforward:

We have a form, an input, and a button. To sum up, we also have a function to handle the form submit and another function to handle the input on change.

The handleNameOnChange will be called every time the user types or removes a character. I wanted to build a local state, a representation of the pokemon name. With this state, we can use it to dispatch when submitting the form.

As we want to try hooks, we will use useState to handle this local state.

const [pokemonName, setPokemonName] = useState(); const handleNameOnChange = (e) => setPokemonName(e.target.value); 

We use the setPokemonName to update the pokemonName every time the user interacts with the input.

And the handleFormSubmit is a function to dispatch the new pokémon to be added to the available list.

const handleFormSubmit = (e) => { e.preventDefault(); addPokemon({ id: generateID(), name: pokemonName }); }; 

addPokemon is the API we will build later. It receives the pokémon's id and name. The name is the local state we defined, pokemonName.

generateID is just a simple function I built to generate a random number. It looks like this:

export const generateID = () => { const a = Math .random() .toString(36) .substring(2, 15) const b = Math .random() .toString(36) .substring(2, 15) return a + b; }; 

addPokemon will be provided by the context API we build. That way, this function can receive the new pokémon and add to the available list. It looks like this:

const addPokemon = (pokemon) => { dispatch({ type: ADD_POKEMON, pokemon }); }; 

It will dispatch this action type ADD_POKEMON and also pass the pokémon.

In our reducer, we add the case for the ADD_POKEMON and handle the state to add the new pokémon to state.

const pokemonReducer = (state, action) => { switch (action.type) { case CAPTURE: return capturePokemon(action.pokemon, state); case RELEASE: return releasePokemon(action.pokemon, state); case ADD_POKEMON: return addPokemon(action.pokemon, state); default: return state; } }; 

And the addPokemon function will be:

const addPokemon = (pokemon, state) => ({ pokemons: [...state.pokemons, pokemon], capturedPokemons: state.capturedPokemons }); 

Another approach is to destructure the state and change only the pokémon's attribute, like this:

const addPokemon = (pokemon, state) => ({ ...state, pokemons: [...state.pokemons, pokemon], }); 

Back to our component, we just need to make sure the useContext provides the addPokemon dispatch API based on the PokemonContext:

const { addPokemon } = useContext(PokemonContext); 

And the whole component looks like this:

import React, { useContext, useState } from 'react'; import { PokemonContext } from './PokemonContext'; import { generateID } from './utils'; const PokemonForm = () => { const [pokemonName, setPokemonName] = useState(); const { addPokemon } = useContext(PokemonContext); const handleNameOnChange = (e) => setPokemonName(e.target.value); const handleFormSubmit = (e) => { e.preventDefault(); addPokemon({ id: generateID(), name: pokemonName }); }; return (     ); }; export default PokemonForm; 

Now we have the available pokémon list, the captured pokémon list, and the third box to create new pokémon.

Pokémon Effects

Now that we have our app almost complete, we can replace the mocked pokémon list with a list of pokémon from the PokéAPI.

So, inside the function component, we can't do any side effects like logging or subscriptions. This is why the useEffect hook exists. With this hook, we can fetch pokémon (a side-effect), and add to the list.

Fetching from the PokéAPI looks like this:

const url = "//pokeapi.co/api/v2/pokemon"; const response = await fetch(url); const data = await response.json(); data.results; // [{ name: 'bulbasaur', url: '//pokeapi.co/api/v2/pokemon/1/' }, ...] 

The results attribute is the list of fetched pokémon. With this data, we will be able to add them to the pokémon list.

Let's get the request code inside useEffect:

useEffect(() => { const fetchPokemons = async () => { const response = await fetch(url); const data = await response.json(); data.results; // update the pokemons list with this data }; fetchPokemons(); }, []); 

To be able to use async-await, we need to create a function and call it later. The empty array is a parameter to make sure useEffect knows the dependencies it will look up to re-run.

The default behavior is to run the effect of every completed render. If we add a dependency to this list, useEffect will only re-run when the dependency changes, instead of running in all completed renders.

Now that we've fetched the pokémon, we need to update the list. It's an action, a new behavior. We need to use the dispatch again, implement a new type in the reducer, and update the state in the context provider.

In PokemonContext, we created the addPokemons function to provide an API to the consuming component using it.

const addPokemons = (pokemons) => { dispatch({ type: ADD_POKEMONS, pokemons }); }; 

It receives pokémon and dispatches a new action: ADD_POKEMONS.

In the reducer, we add this new type, expect the pokémon, and call a function to add the pokémon to the available list state.

const pokemonReducer = (state, action) => { switch (action.type) { case CAPTURE: return capturePokemon(action.pokemon, state); case RELEASE: return releasePokemon(action.pokemon, state); case ADD_POKEMON: return addPokemon(action.pokemon, state); case ADD_POKEMONS: return addPokemons(action.pokemons, state); default: return state; } }; 

The addPokemons function just adds the pokémon to the list:

const addPokemons = (pokemons, state) => ({ pokemons: pokemons, capturedPokemons: state.capturedPokemons }); 

We can refactor this by using state destructuring and the object property value shorthand:

const addPokemons = (pokemons, state) => ({ ...state, pokemons, }); 

As we provide this function API to the consuming component now, we can use the useContext to get it.

const { addPokemons } = useContext(PokemonContext); 

The whole component looks like this:

import React, { useContext, useEffect } from 'react'; import { PokemonContext } from './PokemonContext'; const url = "//pokeapi.co/api/v2/pokemon"; export const PokemonsList = () => { const { state, capture, addPokemons } = useContext(PokemonContext); useEffect(() => { const fetchPokemons = async () => { const response = await fetch(url); const data = await response.json(); addPokemons(data.results); }; fetchPokemons(); }, [addPokemons]); return ( 

Pokemons List

{state.pokemons.map((pokemon) => {pokemon.name} + )} ); }; export default PokemonsList;

Wrapping up

This was my attempt to share what I learned while trying to use hooks in a mini side project.

We learned how to handle local state with useState, building a global state with the Context API, how to rewrite and replace useState with useReducer, and how to do side-effects within useEffect.

Disclaimer: this was just an experimental project for learning purposes. I may not have used best practices for hooks or made them scalable for big projects.

I hope this was good reading! Keep learning and coding!

You can other articles like this on my blog.

My Twitter and Github.

Resources

  • React Docs: Context
  • React Docs: Hooks
  • Pokemon Hooks side-project: source code
  • Learn React by building an App