react-navigation之動態(tài)修改title的內容
本文介紹了react-navigation之動態(tài)修改title的內容,分享給大家,具體如下:
效果圖:

動態(tài)修改title內容:
static navigationOptions = {
title: ({ state }) => `Chat with ${state.params.user}`
};
ps:`Chat with ${state.params.user}` 這里有個注意的地方,是這個符號·而不是單引號‘
index.android.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import {
AppRegistry,
}from 'react-native';
import rootApp from './js/rootApp'
AppRegistry.registerComponent('GankProject', () = >rootApp);
rootApp.js:
/**
* Created by Administrator on 2017/3/31 0031.
*/
'use strict'import React from 'react';
import {
AppRegistry,
Text,
View,
Button,
}
from 'react-native';
import {
StackNavigator
}
from 'react-navigation';
import ChatScreen from './ChatScreen';
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
//設置標題內容 };
render() {
const {
navigate
} = this.props.navigation;
return ( < View > <Text > Hello, Navigation ! </Text>
<Button
onPress={() => navigate('Chat',{user:'Lucy'})}
title="Chat with Lucy"/ > </View>
);
}
}
const SimpleApp = StackNavigator(
{
Home: {screen: HomeScreen},
Chat:{screen:ChatScreen},
}
);
export default SimpleApp;
ChatScreen.js:
/**
* Created by Administrator on 2017/3/31 0031.
*/
'use strict'
import React,{Component}from 'react';
import {View,Text}from 'react-native';
class ChatScreen extends Component {
static navigationOptions = {
title: ({state}) = >`Chat with $ {state.params.user}`
};
render() {
const {params} = this.props.navigation.state;
return ( < View > <Text > Chat with {
params.user
} < /Text> </View > );
}
}
export default ChatScreen;
效果2:

/** * Created by Administrator on 2017/3/31 0031. */
'use strict'
import React, { Component}from 'react';
import {View, Text, Button}from 'react-native';
class ChatScreen extends Component {
static navigationOptions = {
title: ({
state
}) => {
if (state.params.mode === 'info') {
return `${state.params.user}'s Contact Info`;
}
return `Chat with ${state.params.user}`;
},
header: ({state, setParams}) => {
// The navigation prop has functions like setParams, goBack, and navigate.
let right = ( < Button title = {
`${state.params.user}'s info`
}
onPress = {
() => setParams({
mode: 'info'
})
}
/>
);
if (state.params.mode === 'info') {
right = (
<Button
title="Done"
onPress={() => setParams({ mode: 'none' })}
/ >
);
}
return {right};
},
};
render() {
const {
params
} = this.props.navigation.state;
return (
< View >
< Text > Chat with {params.user} < /Text>
</View >
);
}
}
export default ChatScreen;
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
React視頻播放控制組件Video Controls的實現(xiàn)
本文主要介紹了React視頻播放控制組件Video Controls的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-02-02
React實現(xiàn)一個支持動態(tài)插槽的Layout組件
這篇文章主要為大家詳細介紹了如何使用React實現(xiàn)一個支持動態(tài)注冊內容的插槽組件,文中的示例代碼簡潔易懂,有需要的小伙伴可以了解一下2025-02-02
react用Redux中央倉庫實現(xiàn)一個todolist
這篇文章主要為大家詳細介紹了react用Redux中央倉庫實現(xiàn)一個todolist,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09
react國際化化插件react-i18n-auto使用詳解
這篇文章主要介紹了react國際化化插件react-i18n-auto使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
windows下create-react-app 升級至3.3.1版本踩坑記
這篇文章主要介紹了windows下create-react-app 升級至3.3.1版本踩坑記,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02

