0

I'm new to React and TypeScript.

I want to make a simple function that just fires a console.log(); I've gone through a few tutorials but don't really understand.

I've created an interface as such as per the tutorial:

interface ClickHandlerProps {
  onClick; (event: React.SyntheticEvent<Event>, buttonType: string) => void
}

And in the return function I have:

<Button
  onClick={(e) => props.onClick(e, "button1")}
>

But the tutorial kind of ends there. I don't really know how to just fire a console.log().

I know it's a bit vague, but could anyone help me out and point me in the right direction?

Full code:

import React from 'react';
import { Button, Grid } from '@material-ui/core';

interface ClickHandlerProps {
  onClick; (event: React.SyntheticEvent<Event>, buttonType: string) => void
}

const ActionButtons = ({ onClick }: EventHandlerProps) => {

  return (
    <>
      <Grid container spacing={3}>
        <Grid item xs={4}>
          <Button onClick={(e) => props.onClick(e, "button1")} >
            Button 1
          </Button>
        </Grid>
        <Grid item xs={4}>
          <Button onClick={(e) => props.onClick(e, "button2")} >
            Button 2
          </Button>
        </Grid>
      </Grid>
    </>
  );
};

export default ActionButtons;

On index.tsx

import ActionButtons from './ActionButtons';
export default ActionButtons;

2 Answers 2

1

You made a typo here: you must use the colon to separate the property name from its type.

interface ClickHandlerProps {
  onClick: (event: React.SyntheticEvent<Event>, buttonType: string) => void
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. I still get a props is not defined message though. Would you know what that means?
because you are using props in the event handlers, whereas you actually do not need it. Change it to: onClick={(e) => onClick(e, "button1")}
Thanks I did remove that but then it jsut doesn't recognise onClick: onClick is not a function
that depends on how your ActionButtons is called. You should edit the post and add that piece of code.
Thanks - have added above
|
1

Now you can use your ActionButtons component like this:

import React, {SyntheticEvent} from 'react';

const MyComponent = (props) => {
  const clickHandler = (evt: SyntheticEvent<Event>, btnType: string) => {
    console.log(`Button clicked: ${btnType}`
  };

  return <ActionButtons onClick={clickHandler}></ActionButtons>
}

export default MyComponent;

Notice you're passing clickHandler function to onClick input, so any click inside ActionButtons will call clickHandler.

BTW, there seem to be some errors on the example ActionButtons component:

const ActionButtons = ({ onClick }: EventHandlerProps) => {
  return (
    <>
      <Grid container spacing={3}>
        <Grid item xs={4}>
          <Button onClick={(e) => onClick(e, "button1")} >
            Button 1
          </Button>
        </Grid>
        <Grid item xs={4}>
          <Button onClick={(e) => onClick(e, "button2")} >
            Button 2
          </Button>
        </Grid>
      </Grid>
    </>
  );
};

export default ActionButtons;

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.