内容简介:注意:具体代码在参考;1. Button 的click事件中, 如果使用bind(this), 那么 bind(this, 33) 就可以把 33 作为参数传到这个方法中。
注意:具体代码在 https://github.com/sg552/demo_redux_with_async_storage
参考; https://stackoverflow.com/questions/43017807/react-native-onpress-binding-with-an-argument
1. Button 的click事件中, 如果使用bind(this), 那么 bind(this, 33) 就可以把 33 作为参数传到这个方法中。
<Button
title={i18n.t('click_to_change_language', {locale: 'en'})}
onPress={this.changeLanguage.bind(this, 'en')}
>
2. redux中,如果要传递参数,该肿么办呢?
参考; https://stackoverflow.com/a/35769199/445908
2.1 action creator 中定义的方法要有参数,例如:
export const changeLanguage = (new_language) => {
result= {
type: CHANGE_LANGUAGE,
new_language // created a key named "new_language" with value such like 'en', 'zh'
}
return result
}
2.2 reducer 中要有处理的方法,见下面代码中的 action.new_language:
import { INCREASE_COUNT, CHANGE_LANGUAGE } from './types'
const initialState = {
count: 0,
language: 'en'
}
export const countReducer = (state = initialState, action ) => {
switch(action.type){
case INCREASE_COUNT:
console.info("== hi , before +1, count: " + state.count)
return {
count: state.count + 1
}
default:
return state
}
}
export const languageReducer = (state = initialState, action) => {
switch(action.type){
case CHANGE_LANGUAGE:
return {
language: action.new_language
}
default:
return state
}
}
2.3 视图中要传递参数,例如:
changeLanguage(language){
this.props.changeLanguage(language)
}
//...
<Button
title={i18n.t('click_to_change_language', {locale: 'en'})}
onPress={this.changeLanguage.bind(this, 'en')}
>
</Button>
2.4 最后,在mapDispatchToProps中,要修改:
const mapStateToProps = state => {
return {
count: state.countReducer.count,
language: state.languageReducer.language
}
}
const mapDispatchToProps = dispatch => {
return {
increaseCountByOne: () => {
dispatch(increaseCount())
},
// 这里要把这个 new_language方法传递进来,上面的函数用来对比的
changeLanguage: (new_language) => {
dispatch(changeLanguage(new_language))
}
}
}
以上所述就是小编给大家介绍的《react - 20 redux 中传递parameter, click中传递参数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- golang中的函数参数值传递和引用传递
- Python函数中参数是值传递,还是引用传递?
- Golang参数传递问题
- C# 之方法参数传递机制
- python中函数的参数传递
- Flink 中如何解析与传递参数
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Effective Modern C++ 简体中文版
Scott Meyers / 高博 / 中国电力出版社 / 2018-4-23 / 99
想要彻底理解C++11和C++14,不可止步于熟悉它们引入的语言特性(例如,auto型别推导、移动语义、lambda表达式以及并发支持)。挑战在于高效地运用这些特性——从而使你的软件具备正确性、高效率、可维护性和可移植性。这正是这本实用的图书意欲达成的定位。它描述的正是使用C++11和C++14——现代C++来撰写真正卓越的软件之道。 涵盖以下主题: 大括号初始化、noexcept规格......一起来看看 《Effective Modern C++ 简体中文版》 这本书的介绍吧!