简体   繁体   中英

React function with object params throws an unexpected token

I have a problem. I have the following function in React Native.

const HandleConfirmApplication = async (
  opts: { 
    isInvite: boolean,
    confirmationCheckValues: () => any,
    confirmPopUp: () => any,
    loadingApply: () => any,
    decision: () => any,
    updateState: () => any
  }
) => {
  const { 
    isInvite,
    confirmationCheckValues,
    confirmPopUp,
    loadingApply,
    decision,
    updateState 
  } = opts; //then do stuff

When i try to call it, i call it like:

onConfirm={async () =>
                HandleConfirmApplication({ 
                  opts.isInvite: true,
                  opts.confirmationCheckValues: this.props.confirmationCheckValues,
                  opts.confirmPopUp: this.props.onToggleConfirmPopUp,
                  opts.loadingApply: this.props.onToggleLoadingApply,
                  opts.decision: this.handleShiftInviteDecision('ACCEPT'),
                  opts.updateState: null

                })
              }

if i remove the object and just call them normally, it works, however when i tried to update the object so i can make the function params as an object it throws an error on each row saying Unexpected token ',' expected ','

Having a . in a property name isn't valid in JavaScript:

 console.log({ opts.isInvite: 'example' }) 

You should just pass the parameters themselves:

 HandleConfirmApplication({ isInvite: ..., // ... rest of params }) 


Note, technically you can have a . in a property name if you wrap it in quotes but that would give you an incorrect behavior in your particular code because you don't have any properties declared with that format.

 console.log({ 'opts.isInvite': 'this is fine' }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM