经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JSJS库框架 » JavaScript » 查看文章
react-navigation之动态修改title的内容
来源:jb51  时间:2018/9/27 16:26:31  对本文有异议

本文介绍了react-navigation之动态修改title的内容,分享给大家,具体如下:

效果图:


动态修改title内容:

  1. static navigationOptions = {
  2. title: ({ state }) => `Chat with ${state.params.user}`
  3. };

ps:`Chat with ${state.params.user}` 这里有个注意的地方,是这个符号·而不是单引号‘

index.android.js

  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. * @flow
  5. */
  6. import {
  7. AppRegistry,
  8. }from 'react-native';
  9. import rootApp from './js/rootApp'
  10. AppRegistry.registerComponent('GankProject', () = >rootApp);

rootApp.js:

  1. /**
  2. * Created by Administrator on 2017/3/31 0031.
  3. */
  4. 'use strict'import React from 'react';
  5. import {
  6. AppRegistry,
  7. Text,
  8. View,
  9. Button,
  10. }
  11. from 'react-native';
  12. import {
  13. StackNavigator
  14. }
  15. from 'react-navigation';
  16. import ChatScreen from './ChatScreen';
  17. class HomeScreen extends React.Component {
  18. static navigationOptions = {
  19. title: 'Welcome',
  20. //设置标题内容 };
  21. render() {
  22. const {
  23. navigate
  24. } = this.props.navigation;
  25. return ( < View > <Text > Hello, Navigation ! </Text>
  26. <Button
  27. onPress={() => navigate('Chat',{user:'Lucy'})}
  28. title="Chat with Lucy"/ > </View>
  29. );
  30. }
  31. }
  32. const SimpleApp = StackNavigator(
  33. {
  34. Home: {screen: HomeScreen},
  35. Chat:{screen:ChatScreen},
  36. }
  37. );
  38. export default SimpleApp;

ChatScreen.js:

  1. /**
  2. * Created by Administrator on 2017/3/31 0031.
  3. */
  4. 'use strict'
  5. import React,{Component}from 'react';
  6. import {View,Text}from 'react-native';
  7. class ChatScreen extends Component {
  8. static navigationOptions = {
  9. title: ({state}) = >`Chat with $ {state.params.user}`
  10. };
  11. render() {
  12. const {params} = this.props.navigation.state;
  13. return ( < View > <Text > Chat with {
  14. params.user
  15. } < /Text> </View > );
  16. }
  17. }
  18. export default ChatScreen;

效果2:


  1. /** * Created by Administrator on 2017/3/31 0031. */
  2. 'use strict'
  3. import React, { Component}from 'react';
  4. import {View, Text, Button}from 'react-native';
  5. class ChatScreen extends Component {
  6. static navigationOptions = {
  7. title: ({
  8. state
  9. }) => {
  10. if (state.params.mode === 'info') {
  11. return `${state.params.user}'s Contact Info`;
  12. }
  13. return `Chat with ${state.params.user}`;
  14. },
  15. header: ({state, setParams}) => {
  16. // The navigation prop has functions like setParams, goBack, and navigate.
  17. let right = ( < Button title = {
  18. `${state.params.user}'s info`
  19. }
  20. onPress = {
  21. () => setParams({
  22. mode: 'info'
  23. })
  24. }
  25. />
  26. );
  27. if (state.params.mode === 'info') {
  28. right = (
  29. <Button
  30. title="Done"
  31. onPress={() => setParams({ mode: 'none' })}
  32. / >
  33. );
  34. }
  35. return {right};
  36. },
  37. };
  38. render() {
  39. const {
  40. params
  41. } = this.props.navigation.state;
  42. return (
  43. < View >
  44. < Text > Chat with {params.user} < /Text>
  45. </View >
  46. );
  47. }
  48. }
  49. export default ChatScreen;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号