3

I have the following React component:

import { AppInterface } from 'pages/_app'

export default class SiteNavBtn extends React.Component {

    static contextType = AppInterface

    render() {

        const { actions } = this.context 
        const { toggleSiteNav } = actions || {}

        return(
            <button className="site-nav-btn" onClick={ () => toggleSiteNav() }>
                <i className="icon"><span>Open menu</span></i>
            </button>
        )

    }   

}

With the above code, if this.context is undefined and I click on the button, an error complains that toggleSiteNav is not a function (as expected), so I have set an empty function as a default value for it:

const { actions } = this.context 
const { toggleSiteNav = () => {} } = actions || {}

This works, meaning that if this.context is undefined, when I click on the button now nothing happens of course but I don't get an error, however I believe I am invoking an empty function. Is this correct or is there a better way to destructure an object that contains functions and that may be undefined?

3
  • I personally just wouldn't register the event if the callback does not exist. Otherwise, I would make a generic callback that process the click request and handles what to do if the desired callback does not exist. Commented Apr 12, 2019 at 8:06
  • 1
    It seems fine to me. You can just put onClick={toggleSiteNav} in this case. Commented Apr 12, 2019 at 8:07
  • Code seems fine to me and as mentioned this onClick={ () => toggleSiteNav() } can be writte as onClick={toggleSiteNav} Commented Apr 12, 2019 at 8:10

2 Answers 2

1

That is correct, you could however write it like this:

const { actions: { toggleSiteNav = () => {} } = {} } = this.context;
...
<button onClick={toggleSiteNav} ...

Or simply not attach the handler if it is falsy:

const { actions: { toggleSiteNav } = {} } = this.context;
...
<button onClick={toggleSiteNav ? toggleSiteNav : null} ...
Sign up to request clarification or add additional context in comments.

Comments

0

You can move some of your code to a separate function:

import { AppInterface } from 'pages/_app'

 export default class SiteNavBtn extends React.Component {

        static contextType = AppInterface

        toggleSiteNav(){
             const { actions } = this.context 
             const { toggleSiteNav } = actions || {};
             if(typeof toggleSiteNav === 'function'){
                   toggleSiteNav ();
             }

        }


        render() {

            return(
                <button className="site-nav-btn" onClick={ () => this.toggleSiteNav() }>
                    <i className="icon"><span>Open menu</span></i>
                </button>
            )

        }   

    }

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.