<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chris Moore</title>
    <description>The latest articles on DEV Community by Chris Moore (@cwiggo).</description>
    <link>https://dev.to/cwiggo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F389547%2F2a758615-4e62-435b-a0e4-38d99f460473.jpeg</url>
      <title>DEV Community: Chris Moore</title>
      <link>https://dev.to/cwiggo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cwiggo"/>
    <language>en</language>
    <item>
      <title>Using a Custom React based Modal, how can I pass a dynamic triggering function so I can re-use the component?</title>
      <dc:creator>Chris Moore</dc:creator>
      <pubDate>Tue, 19 May 2020 17:49:49 +0000</pubDate>
      <link>https://dev.to/cwiggo/using-a-custom-react-based-modal-how-can-i-pass-a-dynamic-triggering-function-so-i-can-re-use-the-component-7e7</link>
      <guid>https://dev.to/cwiggo/using-a-custom-react-based-modal-how-can-i-pass-a-dynamic-triggering-function-so-i-can-re-use-the-component-7e7</guid>
      <description>&lt;p&gt;I have the following component which makes up my modal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { ModalBody, Button, Alert } from 'bootstrap';
import { AppModalHeader } from '../../common/AppModalHeader';
import ModalWrapper from './ModalWrapper';

const QuestionModal= ({
    title,
    noText = 'No',
    yesText = 'Yes',
    questionText,
    onYesAction
    children
}) =&amp;gt; {
    const { toggle, isOpen, openModal } = useModalForm();
    return (
        &amp;lt;React.Fragment&amp;gt;
            &amp;lt;ModalWrapper className={className} isOpen={isOpen} toggle={toggle}&amp;gt;
                &amp;lt;AppModalHeader toggle={toggle}&amp;gt;{modalTitle}&amp;lt;/AppModalHeader&amp;gt;
                {isOpen ? (
                    &amp;lt;ModalBody&amp;gt;
                        &amp;lt;p&amp;gt;{questionText}&amp;lt;/p&amp;gt;
                        &amp;lt;Button
                            className="float-right"
                            color="primary"
                            onClick={() =&amp;gt; {
                                if (onYesAction !== undefined) {
                                    onYesAction(toggle);
                                }
                            }}
                        &amp;gt;
                            {yesText != null ? yesText : 'Yes'}
                        &amp;lt;/Button&amp;gt;
                    &amp;lt;/ModalBody&amp;gt;
                ) : null}
            &amp;lt;/ModalWrapper&amp;gt;
            {children({
                triggerModal: () =&amp;gt; openModal({ id: undefined }),
                toggle
            })}
        &amp;lt;/React.Fragment&amp;gt;
    );
};

export default QuestionModal;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;br&gt;
I want to use it as such, where I can dynamically choose the name of the trigger that opens the modal:&lt;/p&gt;

&lt;p&gt;In use e.g. (note: the inner question modal would be repeated, used 4 or 5 times in my application):&lt;br&gt;
`&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;....
&amp;lt;QuestionModal
     //....params that match up with above
    &amp;gt;
    {({ triggerModal }) =&amp;gt; (
    &amp;lt;QuestionModal
         //....params that match up with the component
    &amp;gt;
        {({ triggerModal2 }) =&amp;gt; (
        &amp;lt;&amp;gt;
           &amp;lt;Button onClick={()=&amp;gt;triggerModal();}&amp;gt;Trigger Modal 1&amp;lt;/Button&amp;gt;
           &amp;lt;div&amp;gt;
               &amp;lt;Button onClick={()=&amp;gt;triggerModal2();}&amp;gt;Trigger Modal 2&amp;lt;/Button&amp;gt;
           &amp;lt;/div&amp;gt;
        &amp;lt;/&amp;gt;
    &amp;lt;/&amp;gt;
    )}
&amp;lt;/QuestionModal&amp;gt;
....
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;br&gt;
How could I achieve this, by extending the question modal to pass a dynamic function? Just because I keep getting stuck in having to think about duplicating the original component, I want to make this component as reusable as I can. Any help would be greatly appreciated.&lt;/p&gt;

&lt;p&gt;Thanks in advance&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
