score:2
this problem is brought up in the common pitfalls & best practices section of the documentation:
mobx observable objects do not detect or react to property assignments that weren't declared observable before. so mobx observable objects act as records with predefined keys. you can use
extendobservable(target, props)
to introduce new observable properties to an object. however object iterators likefor .. in
orobject.keys()
won't react to this automatically. if you need a dynamically keyed object, for example to store users by id, create observable _map_s using observable.map.
so instead of using extendobservable
on an observable object, you could just add a new key to an observable map.
example
const {observable, action} = mobx;
const {observer} = mobxreact;
const {component} = react;
class teststore {
mysimpleobject = observable.map({});
@action addsimpleproperty = (value) => {
this.mysimpleobject.set(value, {newprop: value});
}
}
@observer
class myview extends component {
constructor(props) {
super(props);
this.handleaddsimpleproperty = this.handleaddsimpleproperty.bind(this);
}
handleaddsimpleproperty(e) {
this.props.mystore.addsimpleproperty("newpropertyvalue");
}
render() {
var simpleobjectstring = this.props.mystore.mysimpleobject.values();
return (
<div>
<h3> simple object</h3>
{simpleobjectstring.map(e => e.newprop)}
<br/>
<button onclick={this.handleaddsimpleproperty}>add simple property</button>
</div>
);
}
}
const store = new teststore();
reactdom.render(<myview mystore={store} />, document.getelementbyid('mount'));
Source: stackoverflow.com
Related Query
- Why is my call to (mobx) extendObservable not triggering a re-render?
- Why does react call render function if I have not changed the reference of the state?
- Why Mobx observer is not triggering re-render on React recurrent component?
- Why can I not call my function inside the render ()?
- Why is onSubmit function not called during render even if we don't use an inline function to call it but onCLick functions are called during render?
- Why is useState not triggering re-render?
- Why I am not able to render my ReactJS application using PhantomJS 2.1.1?
- Why is my React checkbox onChange handler firing on render and then not when the box is clicked?
- Why do componentWillUpdate() and componentWillMount() not trigger render function when setState used in these methods?
- Why is onClick triggering on render of component instead of when clicked?
- How to make MobX update the component when observable is not used in the render method?
- Ajax call in MobX: MobX observer: Store is not available! Make sure it is provided by some Provider
- Why my react html render is not working?
- Why is Fetch API call not returning the response headers?
- Why my react `Carousel` does not render properly?
- Why in React, my axios API call has Authorization Header which contains Bearer <token> but not being authorized and gives 401 error
- Why react not render false
- Why does my HTTP GET call to a subroute in react app gives a 404 NOT Found instead of HTML?
- Making an Axios call within a forEach function to ammend an object is not being recognized in the render despite being in the state
- Why render error in react v18 occures although I do not use legacy code?
- Why is setState(this.state) not triggering a rerender?
- Why react doesn't call render when state is changed?
- Why does not <div style={{ backgroundColor: "black" }}></div> render black but <div style={{ backgroundColor: "black" }}> Hello </div> doesn't?
- Warning: Render methods should be a pure function of props and state; triggering nested component updates from render is not allowed
- React Mobx - Change store from other store not render observer components
- Why child component's render is called when props did not change?
- Why does react component do not re render after changing state
- React/Redux not triggering child render on state update
- React Lifting State Up - Why not call constructor of child when parent rerender?
- Why does storing Open Weather Api fetched data in saved state not render in React JS?
More Query from same tag
- Returning the correct typeof data from redux thunk
- Reduce on TypeScript
- Trying to import a JSON file into React but not working
- npx create-react-app is not creating servicework.js file in src forlder
- React + Firebase Cloud Functions in Typescript fails to deploy
- How to call a NodeJs resource from React Js
- Access history with pages created through render()
- Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined
- ReactJs Lazy loading with conditional
- Loading font icons with React
- Touch Events Broken with ReactJS Upgrade in Chrome Mobile Emulator
- why we use this.initialState = this.state;
- Filtering element in array is not working in Reactjs
- How can I match data from an object and pass it as props to another component that displays dynamically?
- How to pass button text content to another page in React?
- React | Single onchange method for multiple dynamic select box
- How can I access array when I put it in this.state?
- is there a way to access new context api within constructor?
- React.js check if user is logged in after redirect and display a popup
- How can I pass a value into a useState
- google places api text not working in mui TextField
- alternate way to pass the state value of the container in which <FORM/> is rendered to the custom fields of the form(other than formcontext)
- Adding a custom "ALL" (Total) row to the top of an ag-grid that is selectable like any other row
- React - can't map through array - objects are not valid as a React child
- Unable to pass props from parent to child and save it in state of child component
- Retrieving data from state and displaying it in dynamic form in antd
- How can I make jodit textarea dynamic without using useMemo
- Problem with the search function in react
- React Modifying Textarea Values
- Buggy movement of Object in Array with Redux and React