mobx-react icon indicating copy to clipboard operation
mobx-react copied to clipboard

import {observer} from 'mobx-react' not work

Open FunctionRun opened this issue 9 years ago • 6 comments

when I use

//not work
import {observer} from 'mobx-react'   

but use

// work
import {observer} from 'mobx-react/custom'

Why?

My code like this,

'use strict';
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {observable, computed, autorun} from 'mobx';
import {observer} from 'mobx-react';  // not work 
// import {observer} from 'mobx-react/custom'; // work 



class Todo {
    id = Math.random();
    @observable title;

    @observable finished = false;

    constructor(title) {

        this.title = title;
    
    }
}

class TodoList {
    @observable todos = [];
    @computed get unfinishedTodoCount() {

        return this.todos.filter(todo => !todo.finished).length;
    
    }
}


@observer
class TodoListView extends Component {
    addTodo = () => {

        this.props.todoList.todos.push(new Todo(new Date().getTime()));
    
    }
    deleTodo = () => {


    }
    render() {

        console.log('render');
        return (
            <div>
                <ul>
                    {this.props.todoList.todos.map(todo =>
                        <TodoView todo={todo} key={todo.id} />
                    )}
                </ul>

                <h3>Tasks left: {this.props.todoList.unfinishedTodoCount}</h3>

                <button onClick={this.addTodo}> ADD TODO </button>
                <button onClick={this.deleTodo}> DELETE TODO </button>
            </div>
        );
    
    }
}

const TodoView = observer(({todo}) =>
    <li>
        <input
            type='checkbox'
            checked={todo.finished}
            readOnly
            onClick={() => todo.finished = !todo.finished} />

        {todo.title}
    </li>
);

const store = new TodoList();


const todo1 = new Todo('Get Coffee');
const todo2 = new Todo('Write simpler code');

store.todos.push( 
    todo1,
    todo2
);


ReactDOM.render(<TodoListView todoList={store} />, document.querySelector('.todo'));

store.todos[0].finished = true;

FunctionRun avatar Jun 13 '17 13:06 FunctionRun

That is pretty strange, as all those modules are generated the same way. But please provide some more information when filing an issue.

Could you set up a small repo to demonstrate that this no longer works? Do you have a specific webpack alias or something like that in place? Can you elaborate on 'not work'? Build errors, or exceptions? Which versions are you using, etc.

mweststrate avatar Jun 13 '17 13:06 mweststrate

@mweststrate This my package.json

"dependencies": {
   "mobx": "3.1.14",
   "mobx-react": "4.2.1",
   "react": "15.5.4",
   "react-dom": "15.5.4"
 },
 "devDependencies": {
   "babel-cli": "^6.24.1",
   "babel-core": "^6.25.0",
   "babel-loader": "^7.0.0",
   "babel-plugin-transform-decorators-legacy": "^1.3.4",
   "babel-plugin-transform-runtime": "^6.23.0",
   "babel-preset-es2015": "^6.24.1",
   "babel-preset-react": "^6.24.1",
   "babel-preset-stage-0": "^6.24.1",
   "extract-text-webpack-plugin": "^2.1.2",
   "webpack": "^2.2.1",
   "webpack-dev-server": "^2.4.5"
 }

FunctionRun avatar Jun 14 '17 07:06 FunctionRun

@mweststrate update store.todos through the code like this

store.todos[0].finished = true;

but component TodoListView not trigger render again, when I use import {observer} from 'mobx-react' .

FunctionRun avatar Jun 14 '17 07:06 FunctionRun

I resolved this problem through disabled resolve.mainFields in DllPlugin webpack.lib.js . my webpack.config.js like this,

 plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new ExtractTextPlugin('style.css'),
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./manifest.json')
        }),
        new webpack.LoaderOptionsPlugin({
            debug: true
        })
    ]

and my webpack.lib.js like this

const libConfig = {
    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].js',
        library: '[name]'
    },
    entry: {
        libs
    },
    plugins: [
        new webpack.DllPlugin({
            path: 'manifest.json',
            name: '[name]',
            context: __dirname
        })
    ],
    // resolve: {
    //     mainFields: ['browser', 'main']
    // }
};

FunctionRun avatar Jun 15 '17 08:06 FunctionRun

Hi. Even I am facing the same problem. It gives me a build error. Decorators not yet supported. ERROR in ./src/components/List.js Module build failed: SyntaxError: E:/React Space/apollo-app/src/components/List.js: Decorators are not officially supported yet in 6.x pending a proposal update. However, if you need to use them you can install the legacy decorators transform with:

npm install babel-plugin-transform-decorators-legacy --save-dev

and add the following line to your .babelrc file:

{ "plugins": ["transform-decorators-legacy"] }

The repo url is: https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy.

But i have already done the above steps.

jaydeepresources avatar Nov 18 '17 07:11 jaydeepresources

it's crazy,no!

wangyapeng avatar Aug 02 '18 00:08 wangyapeng