Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 17087

Call function with setState from other component

$
0
0

I have the following setup:

import {getNewImage} from '...'

export default class FirstClass extends Component {
    constructor() {
        super();
        this.state = {
            imageURL: 'www.test.com/new.jpg',
        }
    }

    update = () => {
         this.setState({
             imageURL: 'www.test.com/updated.jpg',
         })
    }

    render() {
        return (
            <View>
                <Image
                    source={{ uri: this.state.imageURL }}
                />
            </View>
        );
    }
}
import Class1 from '...'

export default class SecondClass extends Component {
    render() {
        return (
            <TouchableOpacity onPress={() => new FirstClass().update()}>
                <Class1></Class1>
            </TouchableOpacity>
        );
    }
}

The problem is: it doesn't update the imageURL. I tried multiple things. Logging this inside of update gave back the right object. But trying to log the this.setState() gives an undefinded back.


Viewing all articles
Browse latest Browse all 17087

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>