0

So, I have an app that randomly selects pokemon from a list to build a team of pokemon for the user.

I know how to import images to use in a react app. But how would I serve an image to a user using react? I have the sprites located in two separate places currently because I was trying to see if I needed to send from the client or server side. But my file structure is as follows

client/
   src/
     components/
        sprites/
        pokemon.js
server.js
sprites/

When the page loads, it requests a random pokemon from the server. Then this code executes

<h2>Pokemon</h2> 
              <ul>
                {this.state.pokemon.map(pokemon => 
                  <li key={pokemon.id}> <img src={'./sprites/' + pokemon.name + '.png'} alt={pokemon.name}></img> {pokemon.name} {pokemon.galar_dex} </li>
                )}
              </ul>
              <br/>

But instead of showing an image, I am shown the alt text. How would I serve an image to the user this way?

1 Answer 1

1

You can do something like this

<img src={require(`./sprites/${pokemon.name}.png`)} alt={pokemon.name} />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.