Home > OS >  How to combine prop to map state
How to combine prop to map state

Time:01-09

I want dynamic state map. And I'm confused when I try to combine the state map resulting from const with the usual map syntax

My index.js like this

function Index({ data }) {
 const [value, setValue] = useState(0)
 const [days, setDays] = useState({
    0: 'sunday',
    1: 'monday',
    2: 'tuesday',
    3: 'wednesday',
    4: 'thursday',
    5: 'friday',
    6: 'saturday',
    7: 'other',
    8: 'unknown',
 })
 const day = days[value]
 const handleChange = (event, newValue) => {
    setValue(newValue)
  }
 return (
 <>
  <Box>
  <Tabs
    value={value}
    onChange={handleChange}
    ...
  >
   <Tab label='Sunday' />
 ...
 

I want (day) to be dynamic, but I got an error and a state map like this

{data.(day).map((data) => {
      return (
       <ImageListItem
          key={data.mal_id}
          sx={{
            overflow: 'hidden',
            minWidth: { xs: '150px', sm: '200px', md: '200px' },
          }}
       >
      ...

And this the api

{
    "request_hash": "lsbjdlbvajbval",
    "request_cached": true,
    "request_cache_expiry": ...,
    "monday": [
        {
    ...
    }
    ],
    "tuesday": [
        {
    ...
        }
    ],
...

Thanks a lot for the answer

CodePudding user response:

[] brackets can be used to get the dynamic value from a JSON

{data[day].map((data) => {

  •  Tags:  
  • Related