score:2
Accepted answer
class SampleStore {
counter = 0;
constructor(arg) {
makeAutoObservable(this);
}
// Make it arrow function
increment = () => {
this.counter++;
return this.counter;
}
// Make it arrow function
decrement = () => {
this.counter--;
return this.counter;
}
}
score:0
import { useStore } from "../stores";
import { observer } from "mobx-react";
const Index = (props) => {
const {sampleStore:store} = useStore();
return (
<>
<h1>MobX and React.js example</h1>
<p>{store.counter}</p>
<button onClick={store.increment.bind(store)}>+</button>
<button onClick={store.decrement.bind(store)}>-</button>
</>
);
};
export default observer(Index);
score:2
constructor(arg) {
makeAutoObservable(this, {}, { autoBind: true });
}
Source: stackoverflow.com
Related Query
- Mobx makeAutoObservable not binding this
- React - Why is binding this not required in this example?
- setState not a function after binding this
- Functions are not valid as a React child. This may happen if you return a Component instead of from render
- How to fix this error : " Module not found :can't resolve popper.js "
- The token '&&' is not a valid statement separator in this version
- Error: Not implemented: window.scrollTo. How do we remove this error from Jest test?
- React DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node
- How to solve this error Can not find utils in context
- npm WARN deprecated tar@2.2.2: This version of tar is no longer supported, and will not receive security updates. Please upgrade asap
- Why I am getting this error - Error: EPERM: operation not permitted, mkdir 'C:\Users\Aniket' command not found: create-react-app
- How to debug this error: Uncaught (in promise) Error: Objects are not valid as a React child
- React Typescript hook error - This expression is not callable
- React Native "The expo SDK requires Expo to run. .... this code is not running on Expo."
- React Hot Loader v3. this component is not accepted by Hot Loader
- React Mobx - component not updating after store change
- Explanation for this complicated 'is not assignable to type' error in typescript
- Why is this returning the error .contains() is not a function
- React not rerendering after mobx observer change
- CloudFront Error: This XML file does not appear to have any style information associated with it
- This expression is not callable. Not all constituents of type 'string | ((searchTerm: string) => Promise<void>) | []' are callable
- This expression is not callable Type 'String' has no call signatures
- Webpack 4 for SSR, How to solve this ReferenceError: window is not defined?
- React tutorial- why binding this in ajax call
- Not able to import decorate from mobx
- Why is clearTimeout not clearing the timeout in this react component?
- Why is this not scoped in React form onSubmit function?
- bind(): You are binding a component method to the component. React does this for you automatically?
- mobx react action binding
- How to get rid of this - message:{'You are not subscribed to this API.'}.?
More Query from same tag
- react list + map component
- Definition for rule '@typescript-eslint/no-implicit-any' was not found with Storybook js
- Link Component deosn't redirect page
- State is always one step behind (setState async problems) - React js
- How can I convert JSX to JS without npm using a build script?
- ReactJS: Two props and checking propType
- I Get the data inside this.props.children;
- How to do a custom logic and redirect to another pages in react or nextjs with useEffect hook
- how to setup a callback using react/flux
- Do I need componentWillReceiveProps and replaceProps when using ReactJS + Redux?
- React/Redux: How to return two objects in a method that calls an API?
- Handling Date of Birth with <select> fields in redux-form
- How to get data out of mongodb in actual webpage using node js, reactjs, express
- React, styled-components and TypeScript: How to wrap a styled component in a functional component
- Customize MUI Table styles - last-child
- My component is not reading imported Slick CSS
- React leaflet map location glitches when not in App.js
- Render Props - React Route
- Don't break a span, only break a whitespace
- Trigger OnSubmit() With Ref - How to Pass Event?
- React Hooks, useReducer not able to get state
- Date-fns on formatted date?
- JS+React Object not conserved in value attribute of <option> tag
- React: usestate and tab behaviour issue
- Why is the wrong index assigned?
- Push into array of objects with nested level using spread operator
- How to add styles to part of a body text in react
- Hover Effect Over an Attribute
- Mock context provider
- Jest/Enzyme w/ React - "Target container is not a DOM element."