gpt4 book ai didi

javascript - 使用不同的图标重新渲染标题 - React Native

转载 作者:行者123 更新时间:2023-11-28 04:51:49 26 4
gpt4 key购买 nike

我需要帮助,我有一个组件,你的功能是渲染应用程序的标题,带有左右图标,中间是当前页面的标题。但是,我可以重新渲染页面标题,但图标无法重新渲染。我不知道这个解决方案。

标题的 MyCode。

import React, { Component } from 'react';
import ReactNative from 'react-native';
import { Icon, Text } from './../labsoft.ui';
import styles from './styles';
const Header = ReactNative.StyleSheet.flatten(styles.header);
const BoxHeaderFlex = ReactNative.StyleSheet.flatten(styles.boxHeaderFlex);
const BoxHeaderIcon = ReactNative.StyleSheet.flatten(styles.boxHeaderIcon);
const BoxHeaderTouchable = ReactNative.StyleSheet.flatten(styles.BoxHeaderTouchable);
const BoxHeaderTouchableCenter = ReactNative.StyleSheet.flatten(styles.BoxHeaderTouchableCenter);

interface HeaderProperties {
leftAction?: HeaderLeftAction,
rightAction?: HeaderRightAction,
title?: string;
style?: Style;
}

interface HeaderState {
leftAction?: HeaderLeftAction,
rightAction?: HeaderRightAction,
title?: string;
}

interface HeaderLeftAction {
icon: string;
onClick?: () => void
}

interface HeaderRightAction {
icon: string;
onClick?: () => void
}

interface Style { }

export default class HeaderComponent extends Component<HeaderProperties, HeaderState> {

constructor(props: HeaderProperties) {
super(props);

this.state = {
leftAction: this.props.leftAction,
rightAction: this.props.rightAction,
title: this.props.title
}
}

public setLeftAction(action: HeaderLeftAction) {
this.setState({
leftAction: action
});
}



public setRightAction(action: HeaderRightAction) {
this.setState({
rightAction: action
});
}

public setTitle(title: string) {
this.setState({
title: title
});
}


render() {
console.log('props: ', this.props.rightAction.icon);
console.log('state: ', this.state.rightAction.icon);

let iconRight = this.state.rightAction.icon;
let iconLeft = this.state.leftAction.icon;

return (
<ReactNative.View style={[BoxHeaderFlex, { ...this.props.style }]}>


{
this.state.leftAction != null ?

<Icon icon={iconLeft} onPress={this.state.leftAction.onClick} />
:
<ReactNative.TouchableOpacity style={BoxHeaderTouchable}>
<ReactNative.View>
</ReactNative.View>
</ReactNative.TouchableOpacity>
}

{

this.state.title != null ?

<Text style={BoxHeaderTouchableCenter}>{this.state.title}</Text>
:
<Text style={BoxHeaderTouchableCenter} />

}


{
this.state.rightAction != null ?
<Icon icon={iconRight} onPress={this.state.rightAction.onClick} />
:
<ReactNative.TouchableOpacity style={BoxHeaderTouchable}>
<ReactNative.View>
</ReactNative.View>
</ReactNative.TouchableOpacity>
}

</ReactNative.View >
);
}

}

我请求更改其他页面中的图标(例如:地理位置)

import React, { Component } from 'react';
import ReactNative from 'react-native';
import { styles, Container, Text } from './labsoft/labsoft.ui';

import App from "./app";
import { BasicPageProperties, BasicPageState, BasicPage } from './interfaces/generics/basicPage';

export interface GeolocationPageProperties extends BasicPageProperties {
}

export interface GeolocationPageState {
latitude: any,
longitude: any,
address: any,
error: any

}

export default class GeolocationPage extends BasicPage<GeolocationPageProperties, GeolocationPageState> {
constructor(props: GeolocationPageProperties) {
super(props);
this.state = {
latitude: null,
longitude: null,
address: null,
error: null,
};

}


componentWillMount() {
this.app.header.setRightAction({
icon: 'star',
onClick: () => { }
})
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
address: "",
error: null,
});

console.log('http://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + ',' + position.coords.longitude + '&sensor=true');

// fetch('http://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + ',' + position.coords.longitude + '&sensor=true')
// .then((response) => response.json())
// .then((data) => {

// this.setState({
// latitude: position.coords.latitude,
// longitude: position.coords.longitude,
// address: data.results[0].formatted_address,
// error: null,
// });

// })
// .catch((error) => {
// console.error(error);
// });

},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 10000, maximumAge: 1000 },
);
}

render() {
return (
<Container>
<Text>Latitude: {this.state.latitude}</Text>
<Text>Longitude: {this.state.longitude}</Text>
<Text>Endereço: {this.state.address}</Text>
{this.state.error ? <Text>Error: {this.state.error}</Text> : null}
</Container>
);
}
}

以及其他代码,用于请求更改图标,但不起作用

import React, { Component } from 'react';
import ReactNative from 'react-native';
import { Container, Text, Button } from './labsoft/labsoft.ui';

import App from "./app";
import { BasicPageProperties, BasicPageState, BasicPage } from './interfaces/generics/basicPage';

export interface MainProperties extends BasicPageProperties {
}

export interface MainState extends BasicPageState {

}

export default class MainPage extends BasicPage<MainProperties, MainState> {

constructor(props: MainProperties) {
super(props);
}

render() {
return (
<Container>
<Button title="aaa" onPress={() => this.app.openDrawer()} />
<Button title="change right action"
onPress={() => {
this.app.header.setRightAction({
icon: "bars",
onClick: () => {
alert("star");
}
})
}} />
</Container>
);
}
}

最佳答案

当导航器渲染其他页面时,我在标题图标中设置了 null 。它正在工作

关于javascript - 使用不同的图标重新渲染标题 - React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42859847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com