0

I have the an issue where I am trying to generate dynamic checkbox elements from a list of data and there are no checkboxes appearing on the screen, yet there are no errors visible in the console. I am following the pattern I used in a different component to render jsx so I must have a typo but I'm stumped.

Main component SearchPage.js:

  const renderSettingsView = (data) => {
    if (settingsViewVisible) {
      return (
        <SettingsView data={data} />
      )
    } else {
      return null;
    }
  }

  const toggleSettingsView = () => {
    setSettingsViewVisible(!settingsViewVisible);
  }

  const toggleMainInfo = (e) => {
    // setShowMainInfo(!showMainInfo);
    setShowMainInfo(e.target.checked, () => {
      alert(showMainInfo)
    })
  }

  const SETTINGS_DATA = [
    {
      label: 'Main info',
      id: 'main_info',
      callback: toggleMainInfo
    }
  ]


  return (
    <div className="ui container" style={{marginTop: '10px'}}>
      <SearchBar term={term} onTermUpdate={onTermUpdate} />
      {renderRecentSearches()}
      <br/><br/>
      <button onClick={toggleSettingsView}>Settings</button>
      {renderSettingsView(SETTINGS_DATA)}
      <div className="ui segment">
        {renderMainContent()}
      </div>
    </div>
  )

both attempts including the commented out one do not show any checkboxes on screen SettingsView.js:

import React from 'react';

import SettingsCheckbox from './SettingsCheckbox';



const SettingsView = ({data}) => {

  // const renderCheckboxes = () => {
  //   return data.map((checkbox_info) => {
  //     <SettingsCheckbox id="main_info" label="Main info" callback={checkbox_info.callback}/>
  //   });
  // }

  const checkboxes = data.map((checkbox_info) => {
      <SettingsCheckbox id="main_info" label="Main info" callback={checkbox_info.callback}/>
    });

  return (
    <div className="ui segment">
    settings
      {checkboxes}
    </div>
  );
}


export default SettingsView;

SettingsCheckbox.js:

import React from 'react';


const SettingsCheckbox = ({id, label, callback}) => {

  return (
    <div style={{width: '200px'}}>
      <input
        type="checkbox"
        id={id}
        name={id}
        value={id}
        onChange={callback()}  />
      <label for="main_info">{label}</label><br/>
    </div>
  );
}


export default SettingsCheckbox;

How do I map over the data list of objects and render checkboxes based on the data?

codyc4321
  • 9,014
  • 22
  • 92
  • 165
  • 1
    return is missing in this line --> `const checkboxes = data.map((checkbox_info) => { });` – Sarun UK Aug 16 '22 at 08:22
  • 1
    Correct way to use map --> `const checkboxes = data.map((checkbox_info) => { return });` – Sarun UK Aug 16 '22 at 08:22
  • thats strange I saw examples where you just list the component and it doesnt say return. adding return fixes the bug and now the checkbox displays – codyc4321 Aug 16 '22 at 08:30
  • You could do an implicit return like `.map(c => (
    ...
    )` notice `()` instead of `{}` It is the same thing as `.map(c => { return
    ...
    })`
    – Sinan Yaman Aug 16 '22 at 08:39
  • Does this answer your question? [React Js: .map doesnt render anything](https://stackoverflow.com/questions/71327725/react-js-map-doesnt-render-anything) – Sinan Yaman Aug 16 '22 at 08:40

1 Answers1

1

I have tried your code snippet of SettingsView.js and there's a little problem I found. You need to do an implicit return like below.

Try using this code,

const checkboxes = data.map((checkbox_info) => (
    <SettingsCheckbox id="main_info" label="Main info" 
    callback= {checkbox_info.callback}/>
));
    
return (
    <div className="ui segment">
        settings
        {checkboxes}
    </div>
);

Notice that I've changed the data.map((checkbox_info) => {<SettingsCheckbox />}) to data.map((checkbox_info) => (<SettingsCheckbox />)) and using () instead of {}

But if you prefer the explicit way, try using the return statement like bellow.

const checkboxes = data.map((checkbox_info) => {
    return (
        <SettingsCheckbox id="main_info" label="Main info" 
        callback= {checkbox_info.callback}/>
    )
});

return (
    <div className="ui segment">
        settings
        {checkboxes}
    </div>
);

Both way works just fine...

Aushraful
  • 132
  • 1
  • 7