React Native - Navigation + Image

3

I'm having a hard time solving a problem I'm having using the react native lib navigation.

When I open a post, for example, by sending the image parameter to it, it returns me the following error message.

My code is as follows:

render() {
    const {params} = this.props.navigation.state;
    var image = require(images + params.image);
    return (
        <ScrollView style={styles.container}>
            <Image style={styles.cover} source={image}/>
            <Text>{params.title} {image}</Text>
        </ScrollView>
    );
}

Anyone who has gone through this can help me, please?

Thank you.

    
asked by anonymous 13.11.2017 / 00:06

2 answers

2

Hello,

This is a very common error in react-native and in JS itself for some reason require () does not work this way you can make an array of objects here popular and make a map to scroll scroll get the images of the objects from the array and then in time to show a detail screen you can pass the data of your array.

Example using array of objects + react-navigation:

render() {
  const dados = [
    {
      imagem: require('./img1.png'),
      titulo: 'Usuario 1',
    },
    {
      imagem: require('./img2.png'),
      titulo: 'Usuario 2',
    },
    {
      imagem: require('./img3.png'),
      titulo: 'Usuario 3',
    },
    {
      imagem: require('./img4.png'),
      titulo: 'Usuario 4',
    },
  ];
  return (
    <ScrollView>
      {dados.map((dados, i) => (
        <View key={i}>
          <Image source={dados.imagem} />
          <Text>{dados.title}</Text>
          <Button onPress={() => this.props.navigation.navigate('telaExemplo', {
            imagem: dados.imagem,
            titulo: dados.titulo
          })} />
        </View>
      ))}
    </ScrollView>
  )
}
    
05.12.2017 / 14:33
0

You can also solve the problem using Template String .

render() {
    const {params} = this.props.navigation.state;
    var image = require('${params.image}');
    return (
        <ScrollView style={styles.container}>
            <Image style={styles.cover} source={image}/>
            <Text>{params.title} {image}</Text>
        </ScrollView>
    );
}
    
09.03.2018 / 21:39