javascript - 设置为 null 后标题仍然显示
问题描述
我为我的应用程序使用 react-navigation,我已将启动组件设置为 null,但它仍然显示启动组件何时显示,请问我做错了什么。Search for your herbs...
不是为了展示。我正在考虑将状态设置为状态,header: this.state.header
以便在启动组件完成执行后重置状态,即this.setState({header: true})
,这可能吗
飞溅.js
export default class Splash extends React.Component {
static navigationOptions = {
header: null
};
render() {
return (
<View style={styles.container}>
<Image source={require("../logo.png")} />
</View>
);
}
}
class Box extends React.Component {
render() {
return (
<TextInput
placeholder="Search for your herbs..."
underlineColorAndroid={"transparent"}
style={BackStyles.textBox}
/>
);
}
}
主页.js
export default class Home extends React.Component {
static navigationOptions = {
headerTitle: <Box />
};
constructor(props) {
super(props);
this.state = {
record: []
};
}
render() {
setTimeout(() => {
this.setState({ timePassed: true });
}, 4000);
if (!this.state.timePassed) {
return <Splash />;
} else {
const herbs = this.state.record.map(herb => (
<View key={herb.id} style={BackStyles.herb_box}>
<Image
style={BackStyles.image}
source={{ uri: `${herb.name.replace(/ /g, "")}` }}
/>
<View style={{ flexDirection: "column" }}>
<Text style={BackStyles.header}>{herb.name}</Text>
<Text style={BackStyles.sub}>{herb.bot}</Text>
</View>
</View>
));
return (
<View style={BackStyles.main}>
<View style={{ flex: 1 }}>
<ScrollView overScrollMode={"never"}>{herbs}</ScrollView>
</View>
</View>
);
}
}
}
应用程序.js
import { createStackNavigator } from "react-navigation";
const RootStack = createStackNavigator(
{
Home: Home
},
{
initialRouteName: "Home",
navigationOptions: {
headerStyle: {
backgroundColor: "#fff"
},
headerTintColor: "#28B564",
headerTitleStyle: {
fontWeight: "bold"
}
}
}
);
export default class App extends Component {
render() {
return <RootStack />;
}
}
解决方案
我之前遇到过和你类似的问题,我觉得你应该在App.js里面实现splash组件,也就是在.js里面设置timepassed变量的状态APP.js
。所以在时间流逝之后是真的,然后你显示<RootStack/>
import Home from './components/Home';
import Splash from './components/Splash';
import { createStackNavigator } from 'react-navigation';
const RootStack = createStackNavigator(
{
Home: Home
},
{
initialRouteName: 'Home',
navigationOptions: {
headerStyle: {
backgroundColor: '#fff',
},
headerTintColor: '#28B564',
headerTitleStyle: {
fontWeight: 'bold',
},
},
}
);
export default class App extends Component {
constructor(props){
super(props);
this.state = {
timePassed: false,
};
}
render() {
setTimeout(() => {
this.setState({timePassed: true})
}, 4000);
if (!this.state.timePassed) {
return <Splash/>;
} else {
return (
<RootStack/>
);
}
}
}
推荐阅读
- solidity - Free TON Solidity 智能合约中字符串的 sha256 结果不正确
- plotly-dash - 上传一个可编辑的破折号数据表并使用其中的一列作为输入来生成第二个破折号数据表
- python - 此代码示例的时间复杂度是多少?像嵌套循环,但内循环是一个固定数字
- google-custom-search - Google 自定义搜索:可以指定多少个域
- python - pandas dataframe - 如何在json对象中提取特定值
- sql-server - 为什么在 SQL 中使用“小于”哈希索引会变慢
- python - 过滤具有 2 级列标题的 pandas 数据框
- azure - 为 .Net 桌面应用程序执行 Azure Pipeline 时出错 - Azure DevOps
- laravel - 如何将多个 laravel api 放在一个子域上
- java - 如何使用处理创建适当的渐变