score:0
Accepted answer
If you're decided on having an individual modal component that switches behaviour based on state/props you could approach it like this:
const modals = {
type1: {
Header: <div>Modal1 Header</div>,
Content: <div>Modal1 Content</div>,
open: true,
},
type2: {
Header: <div>Modal2 Header</div>,
Content: <div>Modal2 Content</div>,
open: true,
},
};
const Modal = ({type}) => {
const [modalType, setModalType] = useState(type);
const { Header, Content, open } = modals[modalType];
return (
<>
<SemanticModal
Header={Header}
Content={Content}
open={open}
/>
</>
);
};
An alternative to consider is creating a reusable BaseModal
component which you extend into a seperate component for each use case:
const BaseModal = ({ Header, Content, open }) => {
return (
<>
<SemanticModal Header={Header} Content={Content} open={open} />
</>
);
};
const Modal1 = () => {
return (
<BaseModal
Header={<div>Modal1 Header</div>}
Content={<div>Modal1 Content</div>}
open={true}
/>
);
};
The nice thing about this pattern is that you still get to share the common elements of all modals whilst selectively injecting the parts that need to differ
Source: stackoverflow.com
Related Query
- Looking for a clever way to render multiple children component dynamically
- I am looking for a way to fix the first column in a table component in react material-ui
- Best Way to Render Multiple Paragraph Text read from JSON into React Component
- Pascal case error for dynamically adding component name to Reactjs Route's render method
- Is there a way to render a custom react component for all paragraphs tags?
- Multiple path names for a same component in React Router
- Is there a way to render multiple React components in the React.render() function?
- React component render is called multiple times when pushing new URL
- What is the correct way of adding a dependency to react in your package.json for a react component
- React component children detect if empty / null before render
- Using for loops and switch cases in React to dynamically render different components
- Can a react component have multiple areas for child content?
- how to render multiple children without JSX
- How to render React Component into itself, in a recursive way
- Correct way of Creating multiple stores with mobx and injecting it into to a Component - ReactJs
- React Component wait for required props to render
- Reactjs Render component dynamically based on a JSON config
- Is there a better way to pass multiple props down to n children
- How to wait for complete render of React component in Mocha using Enzyme?
- Render multiple items on reactjs component
- Which is the right way to detect first render in a react component
- Proper way of handling conditional component render depending on route?
- Adding a click handler to React children for methods on the child component
- Change the children of a component dynamically in React
- Multiple path names for a same component in Reach Router
- Multiple times render in react functional component with hooks
- Using Ag-Grid Enterprise license getting a 'ag-grid: Looking for component [agSetColumnFilter] but it wasn't found." error
- ReactJS: Prettiest way to dynamically load Component within Modal
- Is there a clean way to conditionally load and render different components for the same React Router route?
- Wait for multiple async calls to finish before React render
More Query from same tag
- requestAnimationFrame not working as expected
- How to redirect to log in page after click logout button on navbar header in React?
- How to map through nested array of objects in React
- React execute common code on each route change
- How to check if prevProps and nextProps are the same in React?
- ReactJS + Redux: How to directly navigate to a page (skip login) if token is in local storage?
- NavBar Not Changing In React
- How can I filter the result of a ternary that's inside a map?
- Passing className prop to Material-ui child/inner elements
- Embedding Google calendar as iframe in a react app?
- Values are not passing and displaying even after using useContext() in react hooks
- How to get data from Api first and then call the my function?
- Flow: React children array error
- observer from 'mobx-react-lite' not working
- useStates seem as undefined on props
- Reactjs useReducer hook error ReferenceError: Cannot access 'reducer' before initialization
- setInterval start and stop behavior
- How to move css-in-js (Styled Components) to an external css files during build using webpack - ReactJS
- What's the difference between onClick ={ () => function()} and onClick = {function()}?
- How to integrate Reselect in my React + Redux application?
- How can I hide header component in Login page
- In NextJS Is it possible to have custom _app.js read a slug, getInitialProps and pass those props to every component including all pages?
- Nextjs: TypeError: unsupported file type: undefined after update to v.11
- Array in React being mutated
- React : Toggling PopUp re-renders entire component
- Get URL Params before server Next js render
- How does react keep variables value in useCallback?
- Firebase function linkAndRetrieveDataWithCredential doesn't work
- Browserify produces an unusable bundle.js
- Issue with react router Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components)