Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加react-hot-loader遇到mirror.model报错,是否可以解决一下呢 #98

Open
zhaoyuchief opened this issue Jul 27, 2018 · 8 comments

Comments

@zhaoyuchief
Copy link

mirror.model是否可以update,添加react-hot-loader遇到mirror.model报错
throw new Error('Model "' + name + '" has been created, please select another name!');

@llh911001
Copy link
Member

这个错误,应该是 react-hot-loader 导致 mirror.model 方法执行了多次引起的。

可以试试将 mirorr.model 方法的调用逻辑,从“展示型组件“的定义逻辑处剥离。

@zhaoyuchief
Copy link
Author

zhaoyuchief commented Jul 27, 2018 via email

@llh911001
Copy link
Member

“更改 model”的具体意思是?

是 “model 的名称不变但数据变了”?如果是,则建议通过 dispatch action 的方法更改;

@zhaoyuchief
Copy link
Author

zhaoyuchief commented Jul 30, 2018 via email

@llh911001
Copy link
Member

llh911001 commented Jul 30, 2018

是在问“如何更改 model 中的数据(即如何 dispatch action)” 是么?
如果是的,则看这里的文档 actions

@zhaoyuchief
Copy link
Author

zhaoyuchief commented Jul 30, 2018 via email

@llh911001
Copy link
Member

嗯,是的。

举个例子:

mirror.model({
  name: 'test'
})

这样一个 mirror.model 的定义,假如你执行多次,是肯定会报错的。因为 mirror 本质上是“单例”的,每个 model 调用,都会将一个 “model 对象”添加到这个单例中,而每个“model 对象”的唯一标识就是 name。同一个 name 被调用多次,是会报错的,这是预期的逻辑。

那么你的问题在于,使用用 hot loader 时触发了“同一个 model 对象被 mirror.model 方法调用多次“的逻辑,因此解决方法就是:hot loader 触发你的组件热更新的时候,不要去触发 mirror.model 的重复调用。

这样描述清楚了么?

@kvkens
Copy link
Contributor

kvkens commented Oct 30, 2018

热更新的时候要判断modal的name是否存在,LZ已经解决了贴下代码吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants