How to skip details from observable mobx to mobx-react?

I can not figure out the mobx reaction ...

How to transfer details from monitored mobx to an observer with mobx support?

The code below doesn't work, but I feel like it should. Can someone tell me what is going wrong?

let mobxData = observable({information: "this is information"});

@observer class Information extends React.Component {
    render() {
        console.log(this.props.mobxData.information);
        return (
            <h1>class: {this.props.mobxData.information}</h1>
        )
    }
};

const StatelessInformation = observer(({mobxData}) => {
    console.log(mobxData.information);
    return <h1>stateless: {mobxData.information}</h1>
});

ReactDOM.render(
    <div>
        <Information/>
        <StatelessInformation/>
    </div>,
    document.getElementById('app')
);
+4
source share
1 answer

I haven’t done a lot of mobs lately and haven’t tested this, but as a rule, you would have a provider somewhere, and then use @injectfor transferring storages as details

Consumer Information:

import { observer, inject } from 'mobx-react'

@inject('information')
@observer
class Information extends React.Component {
  render(){
    {this.props.information.foo}
  }
}

model level is very simple

import { observable, action } from 'mobx'

class Information {
  @observable foo = 'bar'
  @action reset(){
    this.foo = 'foo'
  }
}

export new Information()

Root provider level

import { Provider } from 'mobx-react'
import Information from ./information'

<Provider information={Information}>
  <Information />
</Provider>

// test it... 
setTimeout(() => {
  Information.foo = 'back to foo'
}, 2000)

, , ,

context childContextType contextType, HOC props.

+2

Source: https://habr.com/ru/post/1683897/


All Articles