2

I'm fairly new to Typescript and React. I've been trying to implement the react-rewards npm library and I have solved all except one issue.

type Props = {}

class Surprisebutton extends Component<Props>{
    reward: any;

    render() {
        return (
            <Reward 
            ref={(ref) => { this.reward = ref }}
            type='memphis'>
                <Button onClick={this.reward.rewardMe()} style={styles.button} variant="contained" color="primary">
                    Surprise!
              <FavoriteIcon style={{ marginLeft: 10 }} />
                </Button>
            </Reward>
        )
    }
}

After running npm start I get an error which says TypeError: this.reward is undefined. What is the best way to resolve?

3 Answers 3

1

It has nothing to do with TypeScript. TS is only a compiler and linter and you're getting a runtime error. It's this line:

this.reward.rewardMe()

The ref gets assigned after the component fully mounts and rewardMe() is attempting to call it immediately. This is also a secondary mistake. You don't want to use the () invocation or the function is going to immediately fire (and never stop).

The line should be

<Button onClick={this.reward.rewardMe} style={styles.button} variant="contained" color="primary">
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Andrew! Checking this currently
I get the Property 'reward' does not exist on type 'Surprisebutton'. error when trying this approach as well. Declaring reward as a property by using reward: any; before render seems to get rid of the error in the compiler but when running npm start I still get the TypeError: this.reward is undefined error in browser.
You changed the onClick handler and it still did not work? There is a chance that the library doesn't accept a ref prop. Property 'reward' does not exist on type 'Surprisebutton' is a typescript type definition. TypeError is a javascript error. If you got it immediately without clicking the button, then you forgot to change the click handler
I actually did change the onClick handler. Also, this implementation is directly from their documentation [github.com/thedevelobear/react-rewards] I'm also seeing a suggestion under ref={(ref)... which states Parameter 'ref' implicitly has an 'any' type, but a better type may be inferred from usage.
Excuse me but there is an example using it with functional componets?
0

I might be wrong because I never used this package but according to documentation/Useage topic. They are not initializing reward variable. Try removing that, Here's the fixed version

type Props = {}

class Surprisebutton extends Component<Props>{

render() {
    return (
        <Reward 
        ref={(ref) => { this.reward = ref }}
        type='memphis'>
            <Button onClick={this.reward.rewardMe()} style={styles.button} variant="contained" color="primary">
                Surprise!
          <FavoriteIcon style={{ marginLeft: 10 }} />
            </Button>
        </Reward>
    )
  }
}

Again I might be wrong but according to the github page i thinks that's the only thing i can infer.

2 Comments

Thanks for the insight, will check this and let you know if it works or not
This doesn't seem to work Shivam, I get the Property 'reward' does not exist on type 'Surprisebutton'. error.
0

Change your onClick to something like this:

onClick={() => {this.reward.rewardMe()}}

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.