GithubHelp home page GithubHelp logo

Comments (7)

bvaughn avatar bvaughn commented on May 17, 2024 2

Just published 1.2.0 with support for this but you'll need to switch away from arrow function to a regular function if you want to use it.

from react-error-boundary.

bvaughn avatar bvaughn commented on May 17, 2024

Just released via 1.1.0

Thanks!

from react-error-boundary.

blling avatar blling commented on May 17, 2024

If i want to dispatch an action in ErrorHandler, how to get the redux dispatch? I use this.props.dispatch as follows but it is undefined.

const withConnect = connect(mapStateToProps, mapDispatchToProps);

const withReducer = injectReducer({ key: 'home', reducer });
const withSaga = injectSaga({ key: 'home', saga });

export default compose(
  withReducer,
  withSaga,
  (component) => withErrorBoundary(component
    , ErrorFallback
    , (error) => this.props.dispatch(handleError(error)) // **not work here**
  ), // use ErrorBoundary
  withConnect,
)(HomePage);

Anyone could give me some help? My git branch is here.

from react-error-boundary.

bvaughn avatar bvaughn commented on May 17, 2024

I think there might be some confusion about how arrow function work. They specifically preserve the lexical this when and where They are declared, rather than when they're invoked. For example:

class ExampleClass {
  callMethods() {
    arrowFunction();            // 'this' will be the window object
    regularFunction();          // 'this' will be the window object
    arrowFunction.call(this);   // 'this' will be the window object
    regularFunction.call(this); // 'this' will be the instance of ExampleClass
  }
}

const arrowFunction = () => {
  console.log('arrowFunction', this);
};

function regularFunction() {
  console.log('regularFunction', this);
}

new ExampleClass().callMethods();

Regardless of the type of method you pass in though, the onError callback is invoked as a standalone function inside of react-error-boundary so it won't have a this.

I guess I could change the code to preserve the this, but in that case I think you'd still need to use this.props.children.props.dispatch to get at the dispatch property.

from react-error-boundary.

blling avatar blling commented on May 17, 2024

Wow, so fast, i will try it tomorrow, thanks very much @bvaughn !
If i do not use this is there another good practice?

from react-error-boundary.

bvaughn avatar bvaughn commented on May 17, 2024

You are welcome.

I haven't used Redux in a long time. I'm not sure what the best way to approach this is to be honest.

from react-error-boundary.

blling avatar blling commented on May 17, 2024

Get it, thanks for your great work again. ^_^

from react-error-boundary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.