This is an example of something I'd like to understand better syntactically in JSX.
Problem:
This works:
<button
onClick={ !isRecording ? beginRecording : endRecording } >
</button>
and this works:
<button
onClick={ () => { modalPortal.current.open() } } >
</button>
<Modal ref={modalPortal}>
<h1>Congratulations!</h1>
<p>If this modal opened, you find javascript syntax seamless and intuitive</p>
</Modal>
Together, no bueno.
<button
onClick={!isRecording ? () => {modalPortal.current.open();beginRecording} : endRecording } >
</button>
Error:
react-expected-an-assignment-or-function-call-and-instead-saw-an-expression
Detail:
This is inside a function component. isRecording
& endRecording
etc are are states in an object within the scope of the function component which defines the page I'm on in the app, modalPortal
is a reference:
export default function RecordPage()
{
let [audioURL, isRecording, beginRecording, endRecording, timer] = RecorderContext();
const modalPortal = useRef(null);
...
}
I've also tried various permutations of passing this out to a single function that does the conditional evaluation etc
onClick={ doManyThings() } >
With and without arrows, both kinds of brackets and passing in arguments and without, none of it seems to work. I'd love a clear answer from someone knowledgeable!
References to things I've tried that don't work:
Conditional onClick from ternary
Call multiple functions onClick ReactJS
Setting conditional onClick behaviour in React Component
React: Expected an assignment or function call and instead saw an expression