1
function Header() {
    const first = (e) => {
       var result = new Map()
       axios.post('http://localhost:8000/' + query)
       .then(function(response){
         var content=JSON.parse(JSON.stringify(response)).data
         for (var i=0;i<content.data.length;i++){
           result.set(content.data[i].image_id, content.data[i].caption)
        }

         var key = Array.from(result.keys());  
         var value = Array.from(result.values());   
       
      }).catch(err => {
         console.log("error");
         alert(err);
       });
     };

   const second = () => {}
    

 return (
   <div>
       <button onClick={(e)=> { first(); second(); }}/>
       <img src={require(`img/img${key1}`)} className='modal_img'/>
   </div>
 );
}

export default Header;

'result' is a map, and I am trying to use keys in result.

( Because the keys of result(map) is image number that I am trying to use in img src path. --> ex ) img/img13.jpg )

I know that I have to use state, in order to pass variables. But don't know how to use.

(With code above I am getting this error--> 'key' is not defined no-undef)

1

2 Answers 2

1
function Header() {
    const [keys, setKeys] = useState([]); // defining a state using hook
    
    axios.post('http://localhost:8000/' + query)
   .then(function(response){
       var content=JSON.parse(JSON.stringify(response)).data
       for (var i=0;i<content.data.length;i++){
         result.set(content.data[i].image_id, content.data[i].caption)
       }
       var key = Array.from(result.keys());
       setKeys(key);   // setting the state 
       var value = Array.from(result.values());
   });
}

so, you can use the state keys to access the name of the image wherever you want

you can find more about state hook here

Sign up to request clarification or add additional context in comments.

1 Comment

I tried with this code and when I use key in image like this <img src={img/img${key[0]}.jpg}/> , I still get "'key' is not defined" error
1

Use this code:

import { useState } from "react";

function Header() {
    const [result , setResult] = useState({})
    const first = (e) => {
       
       axios.post('http://localhost:8000/' + query)
       .then(function(response){
         var content=JSON.parse(JSON.stringify(response)).data
         for (var i=0;i<content.data.length;i++){
           setResult({...result, [content.data[i].image_id] :content.data[i].caption})
        }

         var key = Array.from(result.keys());  
         var value = Array.from(result.values());   
       
      }).catch(err => {
         console.log("error");
         alert(err);
       });
     };

   const second = () => {}
    

 return (
   <div>
       <button onClick={(e)=> { first(); second(); }}/>
       <img src={require(`img/img${key1}`)} className='modal_img'/>
   </div>
 );
}

export default Header;

You should also read the docs useState Hook

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.