react-native - 如何在静态字段中使用反应导航发送值?
问题描述
我有一个屏幕<Switch />
使用react navigation
.
我知道如何this.state
通过 this.props.navigation.setParams
.
但我不知道如何将<Switch />
值发送到外部函数。
我用代码试试onValueChange={value => params.handleThis(value)}
handleThis
但就我而言key-value
,它显然无法获得价值。
this.props.navigation.setParams({
handleThis: this.changeSwitch
});
如何将<Switch />
onChange 值发送到外部函数?
这是我的代码:
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
title: 'Title',
headerStyle: {
backgroundColor: ColorSetting.headerColor,
elevation: null,
},
headerTitleStyle: {
fontWeight: '300',
fontFamily: 'FFF Tusj',
fontSize: 18
},
headerRight:
<View style={{ marginRight: 15 }}>
<Switch
onValueChange={value => params.handleThis()}
value={params.switchOn}
onTintColor='#444444'
tintColor='#444444'
/>
</View>,
};
};
constructor(props) {
super(props);
this.state = {
switchOn: false
};
}
componentDidMount() {
this.props.navigation.setParams({
handleThis: this.changeSwitch
});
}
changeSwitch = () => {
const { switchOn, titleVersion } = this.state;
this.props.navigation.setParams({ switchOn: !switchOn });
this.setState({ switchOn: !switchOn });
}
任何帮助,将不胜感激。提前致谢。
解决方案
您可以params.handleThis
用作处理程序,不需要内联函数。
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
title: 'SO Question',
headerStyle: {
elevation: null,
},
headerTitleStyle: {
fontFamily: 'FFF Tusj',
fontSize: 18
},
headerRight:
<View style={{ marginRight: 15 }}>
<Switch
onValueChange={params.handleThis}
value={params.switchOn}
onTintColor='#444444'
tintColor='#444444'
/>
</View>,
};
};
之后changeSwitch
将接收新值作为第一个参数。
changeSwitch = (switchOn) => {
this.props.navigation.setParams({ switchOn });
this.setState({ switchOn });
}
这是一个工作示例
推荐阅读
- perl - 在 perl 中为字符串创建通用正则表达式
- c# - 如何消除这些函数中的冗余代码?
- oracle - 如何在返回选择游标的存储过程中调用其他数据库函数
- macos - 打开控制台应用程序以显示统一日志记录日志
- python - 如何使用 Python 3.7 和 Anaconda 运行 Spyder
- python - OpenCv 和 TensorFlow AttributeError:模块“cv2.dnn”没有属性“readNetFromTensorFlow”
- php - 警告:MyCompany\CmsBundle\Controller\DocumentDuplicateController::__construct 缺少参数 1
- android - 在 iOS 和 Android 中使用亚马逊设备消息(推送通知服务)
- c# - 在不同的空隙中填充结构列表
- tcp - HA 代理 TCP 与原始主机名的平衡