score:2
Accepted answer
You are right in that you need to use componentDidUpdate
to do custom logic when the component has been re-rendered with MobX. The observable values will be synchronously updated, but the component will be re-rendered asynchronously as usual.
Example (JSBin)
@observer
class App extends Component {
@observable value = ''
componentDidMount() {
setTimeout(() => this.value = 'focus', 1000);
}
componentDidUpdate() {
this.ref.focus();
}
render() {
return (
<input
ref={ref => this.ref = ref}
value={this.value}
onChange={e => this.value = e.target.value}
/>
);
}
};
Source: stackoverflow.com
Related Query
- What is the Mobx way to do handle set state with callbacks
- What are the differences when re-rendering after state was set with Hooks compared to the class-based approach?
- ReactJS: What is the correct way to set a state value as array?
- What is the correct way to handle a key event using useEffect() hook which on the other hand triggers local state changes?
- React Hooks - What is the recommended way to initialize state variables with useState() from props
- What is the best way to set initial state to avoid error: "can not read property of undefined"?
- With React what is the best way to handle conditional classes
- What is the correct way to set state in React
- Guess the number with React - what is the proper way of using state in that case
- What is the best way to handle Firestore with React?
- What is the correct way to set State according to fetch data?
- Is there a way to set the initialValues using state with formik?
- What is the correct way to handle an accesstoken with react to secure routes?
- What is the proper way to handle component's state in React?
- What is the best way to set the required attribute based on a state in React?
- What is the best way to deal with a fetch error in react redux?
- What is the best way to add a value to an array in state
- React - What is the best way to handle login and authentication?
- What is the proper way to use React Memo with Flow?
- What is the best way to create a single MobX store for an app?
- What is the right way to use Ionic Native / Cordova Plugins with Ionic (React) & Capacitor?
- What is the best way to test Window Scroll Event Handlers with Enzyme?
- What is the correct way of dispatching an action not tied to a component with Redux?
- What is the way to add horizontal scroll on material-ui table with many columns?
- What is the best way to implement undo state change (undo store/history implementation) in React Redux
- How to optimize React components with React.memo and useCallback when callbacks are changing state in the parent
- What is the 'proper' way to update a react component after an interval with hooks?
- What is the best way to make multiple get request with axios in a loop?
- What is the simplest way of testing that a React Component contains a html node with enzyme?
- What is the better/correct way of using Promise.all with React-Redux-Thunk?
More Query from same tag
- Append unique list items to the DOM in ReactJS
- Undefined 'this' when handing class method to React component
- action undefined at reducer
- React equivalent to ng-model
- Why is child component not rerendering in React?
- TypeError: this.state.employees.map is not a function
- Is there a way to filter an array by values of another array
- Make a material-ui icon bolder
- Addin Id to an array objects in react js
- React import html - Module parse failed: You may need an appropriate loader to handle this file type
- ReactJS: Reset the display of the placeholder inside a select
- Multiple RegEx not working with conditions
- Refresh Cognito access token after adding user to a Cognito
- Conditionally rendering in React.js not working
- If props is missing Should I use memo?
- What happens in Javascript in this particular case (Call by sharing)?
- Routing with an Express API and a ReactJS SPA
- Mixing 2 objects in react
- useRouter query returns undefined inside getServerSideProps
- Render multiple React components into a single DOM element
- how react programmatically focus input
- How to add page number to the URL
- How to remove redundant/unused dependencies from package.json?
- Why query with apollo client on fragment gives me an empty object while api is working fine?
- React - How render a component only on some exact route path?
- Redux questions about the real-world example
- Formik form template for xml document creation
- Font-Awesome icon does not show in my ReactJS website
- How to set up Babel 6 stage 0 with React and Webpack
- React does not render correct on page refresh