0

I have problem, when i passing string variable to function.

I created interface MyMessageProps, which declare message on string,

later function MyMessage use this interface and return with this message.

When i adding React Component and trying add this function to button onClick error appear.

interface MyMessageProps {
  message: string;
}

function MyMessage({ message }: MyMessageProps) {
  return <div>i shall speak! my message is: {message}</div>;
}

class App extends Component {

  render() {

    const variable = 'test';

    return (
      <div>
            <button onClick = {() => MyMessage(variable)}></button>
      </div>
    );
  }
}

export default App;

enter image description here

1
  • 1
    Shouldn't it be const variable = {message: 'test'} instead? That's what the error message implies, anyway. Commented Apr 30, 2019 at 19:06

1 Answer 1

2

Your function is expecting an object like:

const mssg = {
  message: 'MyMessage'
}

Try this:

interface MyMessageProps {
  message: string;
}

function MyMessage({ message }: MyMessageProps) {
  return <div>i shall speak! my message is: {message}</div>;
}

class App extends Component {

  render() {

    const variable = {
        message: 'my test'
     };

    return (
      <div>
            <button onClick = {() => MyMessage(variable)}></button>
      </div>
    );
  }
}

export default App;
Sign up to request clarification or add additional context in comments.

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.