0

I have implemented few components and I need to return it in another component by using the names in JSON.

import * as React from 'react';
import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
import AWSLogs from '../supportProviderTables/AWSLogs.js';
import AnalyticsEvent from '../supportProviderTables/AnalyticsEvent.js';
import Assignment from '../supportProviderLists/Assignment.js';
import AllComponents from '../supportProviderTables';

export default function ColorToggleButton() {
  const [alignment, setAlignment] = React.useState(null);

  const handleChange = (event, newAlignment) => {
    setAlignment(newAlignment);
  };

  const buttonSet = [
    {
        value:"profiles",
        text: "Profiles",
        components: [
            {text: "Assignment"},
            {text: "AnalyticsEvent"},
            {text: "AWSLogs"}
        ]
    }
  ];
  
  return (
      <div>
        <ToggleButtonGroup
            color="primary"
            value={alignment}
            onChange={handleChange}
        >
            {buttonSet.map((item,i) => {
                return <ToggleButton value={item.value}>{item.text}</ToggleButton>
            })}
            
        </ToggleButtonGroup>
        {alignment !== null ? alignment.map((item,i) => {
            return (buttonSet.map((ele,j) => {
                if(item === ele.value){
                    return (ele.components.map(data => {
                        console.log(data.text);
                        const ComponentName = AllComponents[data.text];
                        return <ComponentName />
                    }))
                }
            }))
        }) : <div></div>}
    </div>
  );
}

The output is expected to return all the components relevant like below enter image description here But it doesnot return the components and shows an error as "AWSLogs is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter" Can anyone help me to do this?

C

1
  • Share the content of ../supportProviderTables/AWSLogs.js. It should export AWSLogs as default. Commented Mar 22, 2022 at 16:18

1 Answer 1

1

You can't simply export a string and expect React to know that it's a component. The bare minimum is that you have to create a key-value pair, where you manually map each string to the component you want to return.

const components = {
'A':<A/>,
'B': <B/>,
'C': <C/>,
}

So, then when you get the ComponentName in your component, you can return it like

return components[ComponentName], which will return the respective React component.

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

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.