Volodumr
Volodumr

Reputation: 173

How can I pass function through Link in React?

I have to pass function through Link to another component.

    testFavorite=()=>{
        console.log("work")
    }

    <Link 
       to={{
       pathname: '/popular',
         state:{
            testFavorite: this.testFavorite
         }
       }}>
   Popular</Link>

This is how I call a function

this.props.location.state.testFavorite();

But i have this error

history.js:370 Uncaught DOMException: Failed to execute 'pushState' on 'History': function () { console.log("work"); } could not be cloned.

How can i fix it? Thank you a lot

Edit blazing-resonance-qgi2m

Upvotes: 0

Views: 2930

Answers (1)

Umar Javed
Umar Javed

Reputation: 59

It can be done when you replace state with Data.

<Link 
       to={{
       pathname: '/popular',
         data:{
            testFavorite: this.testFavorite
         }
       }}>

Also replace here

this.props.location.state.testFavorite();

Meanwhile, if you also want to pass data as props you can use state along with data

Upvotes: 2

Related Questions