React Native - Pass data to another component

0

I'm having a question, I'm starting with react native and I'm building an app that contains some components and uses react-navigation. The question is:

From this component that renders several items, when I click I want to receive this data in the component DetailsItem.js.

import React, { Component } from 'React';
import {
    ScrollView,
    View,
    StyleSheet,
    TouchableOpacity,
    ActivityIndicator,
} from 'react-native';
import axios from 'axios';
import Itens from './Itens';
import InputSearch from './InputSearch';

export default class ListaItens extends Component {
    constructor(props){
        super(props);

        this.state = { listaItens: [] };
    }

    componentWillMount() {
        axios.get('http://ourikas.github.io/companies.json')
            .then(dados => this.setState({listaItens: dados.data}))
            .catch(() => this.setState('erro ao recuperar dados'));
    }

    onLearnMore = (item) => {
        this.props.navigation.navigate('DetalhesItem', { ...item });
    };

    render() {

        if(this.state.listaItens.length > 0){
        return(
            <View>
                <View>
                    <InputSearch />
                </View>

                <ScrollView>

                    {this.state.listaItens.map((item, i) => {
                        return (<View key={i}>
                            <TouchableOpacity onPress={() => this.props.navigation.navigate('DetalhesItem', this.onLearnMore(item))}>
                            <Itens key={item.id} item={item} />
                            </TouchableOpacity>
                        </View>)
                    })}
                </ScrollView>
            </View>

        );
    } return(<ActivityIndicator size="large" color="#666" style={styles.loadding} />);
    }
}

const styles = StyleSheet.create({
    loadding: {
        marginTop: 250
    }
});

Here is the DetailsItem.js component

import React, { Component } from 'react';
import { Text } from 'react-native';

export default class DetalhesItem extends Component {

    constructor(props){
        super(props);
    }

    render(){

        const { name, email } = this.props.navigation.state.params;

        return(
            <Text>{ alert(email) }</Text>
        );
    }
}


Não sei se vai precisar, mais aqui esta o código do App.js

    import React, { Component } from 'react';
import {
  Platform,
  View,
} from 'react-native';
import { StackNavigator, TabNavigator } from 'react-navigation';
import Icon from 'react-native-vector-icons';

import ListaItens from './src/components/ListaItens';
import DetalhesItem from './src/components/DetalhesItem';
import Categorias from './src/components/Categorias';

export default class App extends Component {
  render() {
    return (
      <Cenas />
    );
  }
}

const Cenas = StackNavigator({
    ListaItens: {
      screen: ListaItens,
      navigationOptions: {
        title: 'Bodokas',
      },
    },

    DetalhesItem: {
      screen: DetalhesItem,
      navigationOptions: {
        title: 'voltar',
      },
    }
});


// const TabNav = TabNavigator({

//   ListaItens: {
//     screen: ListaItens,
//     navigationOptions: {
//       tabBarLabel: 'todos',
//     }
//   },

//   Categorias: {
//     screen: Categorias,
//     navigationOptions: {
//       tabBarLabel: 'Categorias',
//     }
//   },

//   DetalhesItem: {
//     screen: DetalhesItem,
//     navigationOptions: {
//       tabBarLabel: 'mais',
//     }
//   }
// }, {
//   tabBarPosition: 'bottom'
// });

    
asked by anonymous 05.02.2018 / 22:28

2 answers

1

Talk to people, with the help of 2 people from the React Native Brazil group, I was able to solve this problem.

I'll leave the code here for you to base or resolve if you have the same problem.

ListItems:

onLearnMore = (item) => {
    this.props.navigation.navigate('DetalhesItem', item);
};

render() {

    if(this.state.listaItens.length > 0){
    return(
        <View>
            <View>
                <InputSearch />
            </View>

            <ScrollView>

                {this.state.listaItens.map((item, i) => {
                    return (<View key={i}>
                        <TouchableOpacity onPress={() => this.onLearnMore(item)}>
                        <Itens key={item.id} item={item} />
                        </TouchableOpacity>
                    </View>)
                })}
            </ScrollView>
        </View>

DetailsItem:

import React, { Component } from 'react';
import { Text, View, StyleSheet } from 'react-native';

export default class DetalhesItem extends Component {

constructor(props){
    super(props);
}

render(){

    const params = this.props.navigation.state.params;


    return(
        <View>
            <Text>{ params.name }</Text>
            <Text>{ params.description }</Text>
            <Text>{ params.contact.email }</Text>
        </View>
    );
}

}

App.js:

import React, { Component } from 'react';
import {


Platform,
  View,
} from 'react-native';
import { StackNavigator, TabNavigator } from 'react-navigation';
import Icon from 'react-native-vector-icons';

import ListaItens from './src/components/ListaItens';
import DetalhesItem from './src/components/DetalhesItem';
import Categorias from './src/components/Categorias';

export default class App extends Component {
  render() {
    return (
      <Cenas />
    );
  }
}

const Cenas = StackNavigator({
    ListaItens: {
      screen: ListaItens,
      navigationOptions: {
        title: 'Bodokas',
      },
    },

    DetalhesItem: {
      screen: DetalhesItem,
      navigationOptions: {
        title: 'voltar',
      },
    }
});


// const TabNav = TabNavigator({

//   ListaItens: {
//     screen: ListaItens,
//     navigationOptions: {
//       tabBarLabel: 'todos',
//     }
//   },

//   Categorias: {
//     screen: Categorias,
//     navigationOptions: {
//       tabBarLabel: 'Categorias',
//     }
//   },

//   DetalhesItem: {
//     screen: DetalhesItem,
//     navigationOptions: {
//       tabBarLabel: 'mais',
//     }
//   }
// }, {
//   tabBarPosition: 'bottom'
// });

Possibly the errors were occurring because I was using the TabNavigator instead of the Stack and also had errors of being passing an object an object as a parameter for the navigate, so doing the function out solved it.

    
06.02.2018 / 21:19
0

use the function: getParam (ParameterName, defaultValue).

this way: this.props.navigation.getParam (paramName, defaultValue).

If using directly use the: "const {name, email} = this.props.navigation.state.params;" and there are no parameters called, the application will break.

    
11.06.2018 / 16:01