Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditional rendering inside map function ReactJs

I know there’s similar thread already but i’m a total newbie and i can’t work it. I’m not able include conditional rendering after my map function, do i need some sortf of HTML-tag inbetween?

     const renderTableData = () => { 
    let id = 1;
    const activeButton = () => { 
    
    }
    return (
      <tr>
        {days.map((val) => (
            <td>
                 {timeSlot.map((n, i) => ( 
                    
                //     if(freeSlotsList.includes(id)){     THIS CODE SNIPPET
               //         return (<h1>Testing</h1>           DON'T WORK
              //          )}
                    
                 
              <button id={id++} className={activeButton}> {n} {id}</button> 
                 ))}
            </td>
        ))}
      </tr>
    );
  };

EDIT:

i can’t seem to access my freeSlotsList, it looks like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const [freeSlotsList, setFreeSlotsList] = useState([])

useEffect(() => {
Axios.post('http://localhost:3001/api/get/week1/ex').then((response) => {
    setFreeSlotsList(response.data)

    
  })
}, [])
  

>Solution :

Try this, You need to wrap your code into {} if you want to write js code. As () will simply return that as jsx.

const renderTableData = () => { 
let id = 1;
const activeButton = () => { 

}
return (
  <tr>
    {days.map((val) => (
        <td>
             {timeSlot.map((n, i) => {
                
                 if(freeSlotsList.includes(id)){     
                    return (<h1>Testing</h1>         
                    )}
                
             
          return <button id={id++} className={activeButton}> {n} {id}</button> 
             )}}
        </td>
    ))}
  </tr>
);
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading