0
I’m having a question, I’m starting with React On and I’m building an app that contains some components and uses React-navigation. The doubt is as follows:
From this component that renders several items, when I click I want to receive this data in the Detail component.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 component Detail.js
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'
// });
"when you click", click where ? I don’t use React Active, I only use the common web React, but if Voce wants to pass data from one Component to the other could use Redux, I’m wrong ? I speak because I don’t know how it works right in React
– Alex
When you click here: {this.state.listItens.map((item) => { Return (<View key={item.id}> <Touchableopacity onPress={() => this.props.navigation.navigate('Detailsesitem', {item: item.id})}> <Items key={item.id} item={item} /> </Touchableopacity> </View>) })} I wanted to pass the information of this variable item that is coming from a request to the Detail component.
– Henrique Nascimento
is... in my case, React Web I would use Redux, I’m trying to help because I don’t know anything of React-Native but it is very similar to the web.
DetalhesItem
andListaItems
are being loaded insideApp.js
as brothers– Alex
@Marcelorafael Right, I’ll take a look here at the issue of Redux. Thank you!
– Henrique Nascimento