@pcdesign

Как передать state в navigation?

Картинка:
5cfe38823fd52420330071.png
Снэк:
https://snack.expo.io/@aimodding/clock2head

В файле AppbarExample.js я объявил state:
state = { str: 'hello' };

Как передавать этот стэйт в шапку вместо subtitle?

Вот сам код
/* @flow */

import * as React from 'react';
import { View, Platform, StyleSheet } from 'react-native';
import {
  Colors,
  Appbar,
  Paragraph,
  withTheme,
} from 'react-native-paper';
import type { Theme } from 'react-native-paper/types';

type Props = {
  navigation: any,
  theme: Theme,
};

const initialParams = {
  showLeftIcon: true,
  showSubtitle: true,
  showSearchIcon: true,
  showMoreIcon: true,
};

const MORE_ICON = Platform.OS === 'ios' ? 'more-horiz' : 'more-vert';

class AppbarExample extends React.Component<Props> {

  static navigationOptions = ({ navigation }) => {
    const params = { ...initialParams, ...navigation.state.params };

    return {
      header: (
        <Appbar.Header>
          {params.showLeftIcon && (
            <Appbar.BackAction onPress={() => navigation.goBack()} />
          )}
          <Appbar.Content
            title="Title"
            subtitle={params.showSubtitle ? 'subtitle' : null}
          />
          {params.showSearchIcon && (
            <Appbar.Action icon="search" onPress={() => {}} />
          )}
          {params.showMoreIcon && (
            <Appbar.Action icon={MORE_ICON} onPress={() => {}} />
          )}
        </Appbar.Header>
      ),
    };
  };

  state = { str: 'hello' };

  render() {
    const {
      navigation,
      theme: {
        colors: { background },
      },
    } = this.props;
    const params = { ...initialParams, ...navigation.state.params };


    return (
      <View
        style={[
          styles.container,
          {
            backgroundColor: background,
          },
        ]}>
        <Paragraph>{this.state.str}</Paragraph>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: Colors.white,
    paddingVertical: 8,
  },
});

export default withTheme(AppbarExample);

  • Вопрос задан
  • 246 просмотров
Решения вопроса 1
vadimushka_d
@vadimushka_d
Учусь на программиста
В конструкторе можно применить что типа такой штуки
this.props.navigation.setParams({
     makeSaveRequestCallback: this.makeSaveRequest.bind(this)
 });


А в статическом методе к этому пропуск можно обратиться так, navigation.state.makeSaveRequestCallback();

Так же работает и с обычными параметрами
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы