有人可以解释为什么我在这里没有得到预期的结果吗?
const initialState = {
items: ['cats'],
};
const reducer = (state = initialState) => {
return {
...state,
items: state.items.push('dogs'),
};
};
console.log(reducer()); // { items: 2 }
预期结果:{ items: ['cats', 'dogs'] }
实际结果:{ items: 2 }
PS:我想我在js / redux上跳过了这门课程,否则我的大脑不再起作用了。
答案 0 :(得分:3)
您直接用const reducer = (state = initialState) => {
return {
...state,
items: [...state.items, 'dogs'],
};
};
来改变状态,而是使用数组扩展:
private lateinit var customTitle: AppCompatTextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// stuff here
customTitle = createCustomTitleTextView()
// other stuff here
}
private fun createCustomTitleTextView(): AppCompatTextView {
val mTitleTextView = AppCompatTextView(this)
TextViewCompat.setTextAppearance(mTitleTextView, R.style.your_style_or_null);
val layoutParams = ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT
)
layoutParams.gravity = Gravity.CENTER
supportActionBar?.setCustomView(mTitleTextView, layoutParams)
supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
return mTitleTextView
}
override fun setTitle(title: CharSequence?) {
customTitle.text = title
}
override fun setTitle(titleId: Int) {
customTitle.text = getString(titleId)
}