score:1
With Mobx 6, decorators are becoming more discouraged and cumbersome to use (requiring makeObservable(this) to be called carefully in the constructor, even in subclasses.)
I therefore now find it cleaner to use
doStuff = action(() => {
// stuff logic
})
rather than
@action.bound
doStuff() { ...
or
@action
doStuff = () => { ...
This pattern with no decorators also works in older Mobx versions.
score:4
Since there is 2018, the best practice in React apps development is to use lambda functions as class properties instead of class methods.
The lambda function as class property resolves all issues that can happen with context. You don't have to bind methods to the specific context, if using it.
For example, you working with this
in some class method:
export default class SomeClass {
myProp = "kappa"
myMethod() {
console.log(this.myProp)
}
}
In this case, if you will use it, e.g., like some event listener, this
will be unexpectedly (actually, more than expected) change from SomeClass
instance to other value. So, if you using class methods, you should modify you code like this:
export default class SomeClass {
constructor() {
this.myMethod = this.myMethod.bind(this)
}
myProp = "kappa"
myMethod() {
console.log(this.myProp)
}
}
In constructor you are binding your class method to context of SomeClass
instance.
The best way to avoid this kind of unnecessary code (imagine, that you have 10+ of this type of methods - and you should bind each of them), is to simply use lambda functions:
export default class SomeClass {
myProp = "kappa"
myMethod = () => {
console.log(this.myProp)
}
}
That's it! Lambda functions have no context, so this
will always point to the SomeClass
instance. So, now you can decorate you class property as you wish:
export default class SomeClass {
myProp = "kappa"
@action
myMethod = () => {
console.log(this.myProp)
}
}
Note, that if you are using Babel, you have to use transform-class-properties
plugin.
This question is more related to the core of JavaScript, so I advise you to read this MDN article for more information about this
behavior.
Hope, this was helpful!
score:6
You can either use @action.bound
decorator:
@action.bound
doSomething(){
// logic
}
or use labmda function which will preserve the context:
@action
doSomething = ()=> {
// logic
}
Source: stackoverflow.com
Related Query
- mobx react action binding
- Why mobX action doesn't trigger rerendering of React component
- binding input to variable inside a React Dumb Component with MobX
- React router redirect after action redux
- Data binding in React
- Set loading state before and after an action in a React class component
- Binding vs Arrow-function (in JavaScript, or for react onClick)
- How to set/change Field value from external user action 🏁 React Final Form
- React hooks: dispatch action from useEffect
- Cannot read property '.then' of undefined when testing async action creators with redux and react
- React Redux dispatch action after another action
- Performance implications of implementing 2-way data binding in React
- Calling an action from a different reducer with React and redux
- Can I send an AJAX call in React and Redux without action creators and reducers?
- mobx + react unexpected token
- React Dev Tools: "parse hook names" action throws a "Hook parsing failed" error
- React Native Navigation Error: The action navigate with payload {"name": 192.168.100.189:19000", "params":{}} was not handled by any navigator
- using mobx with react functional components and without decorators
- What's the proper way of binding touchstart on React JS?
- Why does React warn me against binding a component method to the object?
- Update React component by dispatching action from non-react component
- React navigation 5 error Binding element 'navigation' implicitly has an 'any' type.ts
- React - Jest - Test preventDefault Action
- React Redux : Action creator not calling reducer
- React Mobx - component not updating after store change
- React - binding `this` to an imported function
- How to change name and Icon of Action Column in Material-Table React
- how should I build onClick action in my react component + Redux
- redirect from redux action in react router v4
- React not rerendering after mobx observer change
More Query from same tag
- React: Refresh component without reload after deleting an item
- Reactstrap navbar toggle not toggling in NextJS TypeScript project
- Objects are not valid as a React child (found: [object Promise]). But I am not returning a promise?
- How to set state of an Array of objects using useState?
- ReactJS custom cursor effect:Cursor not working when user scroll
- What is the best way to use a different API URL in production for a create-react-app using docker?
- React-Spring: Trail inside UseTransition map not working
- Variable substitution parsing error (unexpected token, expected "...")
- How to separate home page content from the rest of the pages in react router?
- Using one instead of multiple makeStyles hooks
- React Chartjs - Update Chart on interval
- how to map() in JSX ? ERROR: Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
- How to share one dialog window for loading different data across multiple child components
- Can't access state from parent component method
- how to fix this error while installing radium packages in vs code terminal?? thanks
- ReactJS not rendering correct items after splice
- How do I use a dynamic JS library (wavesurfer.js) in a functional React component?
- Disable table row on another's selection
- Store fetch data in a variable and use entire json as a prop in a react component
- React open dropDown on top or bottom of dropDown input based on the element's position
- lodash debouce isn't debouncing function is called many times insead of just once, react hooks react native
- Typescript React types not recognised when compiling
- Why is response.data an html string instead of json object? node.js, express.js, react
- handling multiple protected routes
- reload component - React
- hide array of objects based on route variable
- How to use Webpack Module Federation plugin with create-react-app without ejecting
- npm start not running in docker
- React firebase auth() setState not working inside then()
- Radio buttons not visibly changing upon click in React