React-native: Load Images dynamically from state

Created on 30 Oct 2015  ·  3Comments  ·  Source: facebook/react-native

Hi everybody,

I have an issue where I try to change the image source from a change in the state object but it's not changing.
I have logged the value and it's changing but the image does not.

Here it's the code im using:

render() {
        var images = {
            playcircle: require('image!playcircle'),
            pausecircle: require('image!pausecircle')
        }
        return (
            <View style={[styles.container, {width: this.props.width - 10}]}>
                <TouchableOpacity onPress={this._playAudio.bind(this)}>
                    <Image source={images[this.state.playImage]} style={styles.img} />
                </TouchableOpacity>
                <View style={styles.textWrapper}>
                    <Text style={styles.txt}>{this.props.podTitle}</Text>
                </View>
            </View>
        );
    }
_playAudio() {
        var blockThis = this;
        debugger;
        if (!this.state.isPlaying) {
            JSAudioPlayer.play(this.props.url, true, function(error){
                if (error) {
                    alert(error);
                } else {
                    blockThis.setState({
                        isPlaying: true,
                        playImage: 'pausecircle'
                    })
                }
            });
        } else {
            JSAudioPlayer.pauseCurrent();
            blockThis.setState({
                isPlaying: false,
                playImage: 'playcircle'
            });
        }       
    }

Any help will be appreciated.
Thanks in advance

@josev55

Ran Commands Locked

Most helpful comment

I got something similar some time ago. Try to add the key property to the image tag like <Image key={images[this.state.playImage].uri} ... />. This worked for me in this case https://github.com/facebook/react-native/issues/3471

All 3 comments

I got something similar some time ago. Try to add the key property to the image tag like <Image key={images[this.state.playImage].uri} ... />. This worked for me in this case https://github.com/facebook/react-native/issues/3471

@facebook-github-bot stack-overflow

Hey @josev55 and thanks for posting this! @cosmith tells me this issue looks like a question that would be best asked on StackOverflow. StackOverflow is amazing for Q&A: it has a reputation system, voting, the ability to mark a question as answered. Because of the reputation system it is likely the community will see and answer your question there. This also helps us use the GitHub bug tracker for bugs only. Will close this as this is really a question that should be asked on SO.

Was this page helpful?
0 / 5 - 0 ratings