0

I am having trouble displaying my yogaCard in the return statement. When I call it as {yogaCard} I get a message saying it is undefined.

  if (yogas.length > 0) {

                let yogaCard = yogas.filter((yoga) => {
                        if (searchPose === "") {
                            return yoga
                        }
                        else if (
                            yoga.english_name.toLowerCase().includes(searchPose.toLocaleLowerCase())) {
                                return yoga
                            }
                    }).map((yoga) => (
                   <div key={yoga.id} style={{width: '20%' }} className="card"  
                    >
                    <img className='yogaphoto' src={yoga.img_url}/>
                    <div className="card-body">
                      <h2>{yoga.english_name}</h2>

                    </div>
                  </div>
                    ))
            }
 return (
            <>           
<div className="yoga"> 
    <div className='yogasearch'>
           <h1>Yoga Poses</h1> 
           <input type="text" class="input" placeholder="Search by pose" onChange={event => {setSearchPose(event.target.value)}}/>

    </div>

        <div className="row">
             {yogaCard}
        </div>

    
</div>

            </>
        )    

    }
        
    
    export default IndexYogas

3
  • Can you format your code, so it is actually readable? No one is going to have a look at this if it is not formatted properly. Commented Sep 7, 2022 at 22:49
  • Also this is not the whole component, please show us more and tell what you have tried so far. Commented Sep 7, 2022 at 22:49
  • pls, write with state and js file in different segments. Then it would be readable and understandable to anyone. Thanks Commented Sep 8, 2022 at 1:36

1 Answer 1

1

You're not showing how you're retrieving your yogas data from axios or any way to debug your current issue, but I'd recommend passing your data retrieved from axios to a child component that contains all those div boxes and formatting, etc.

function MainPage() {
    const [yogas, setYogas] = React.useState([]);
useEffect(() => {
        // axios call to populate data
        requestYogaInfo().then((resp) => {
            setYogas(resp.data); // this depends on the shape of your webservice response
        });
    }, []);


return (
        <>           
        <div className="yoga"> 
             <div className='yogasearch'>
               <h1>Yoga Poses</h1> 
               <input type="text" class="input" placeholder="Search by pose" onChange={event => {setSearchPose(event.target.value)}}/>
             </div>
             <div className="row">
                 {yogas && yogas.length > 0
                     ? (
                     yogas.filter((yoga) => {
                        if (searchPose === "") {
                            return yoga
                        }
                        else if (
                     yoga.english_name.toLowerCase().includes(searchPose.toLocaleLowerCase())) {
                                return yoga
                            }
                    }).map((yoga) => (
                   <div key={yoga.id} style={{width: '20%' }} className="card"  
                    >
                    <img className='yogaphoto' src={yoga.img_url}/>
                    <div className="card-body">
                      <h2>{yoga.english_name}</h2>

                    </div>
                  </div>
                    ))
                     )
                     : null
                 }
             </div>
        </div>
        </>
        )  
}
Sign up to request clarification or add additional context in comments.

1 Comment

If this doesn't work, please provide a working code sandbox so that any issues can be properly debugged.

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.