例如,当我尝试破坏 Store 的参数 useObserver 不工作时,我有 Store 和 App 当 Store 的标题更改时,React 不会重新渲染应用程序。但是,如果我不破坏 params 并直接使用 Store.title 作为回报,它将起作用。为什么它会这样工作以及如何解决它?
-- Store.js --
class LoginActivity : AppCompatActivity() {
private val userViewModel: UserViewModel by viewModels {
UserViewModelFactory((application as MyApplication).userRepository)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
btnLogin.setOnClickListener {
val email=editEmail.text.toString()
val password=editPassword.text.toString()
/**
* code to check whether user with this email and password exists or not
*/
}
}
}
-- App.js-- 不工作
import { observable, action } from 'mobx'
class Store{
@observable title = "";
@action handleTitleChange({ target: {value} }){
this.title = value;
}
}
-- App2.js-- 工作
import React from 'react'
import { useObserver } from 'mobx-react'
import Store from 'store'
export default () => {
const { title, handleTitleChange } = Store
return useObserver(() =>{
<input value={title} onChange={handleTitleChange}>
})
}