0

I am trying to change my view backgroundcolor with a button click. But nothing happens(no error).

export default class App extends React.Component{

    state = {
        color: "#fff"
        }

        render() {
            
            return(
                
                <View style={{backgroundColor: this.state.color, flex: 1, alignItems:'center', justifyContent:'center',}}>
                  
                  <Button  onClick={() => { this.setState({color: "#00ff00" }) }} title="selam"></Button>

                </View>
                      
            );
        }
};
2
  • this code looks find and should work, did you try to change to a color name not hex value like ` color: "red"` Commented Jul 18, 2020 at 13:52
  • Yes but thats not working either Commented Jul 18, 2020 at 13:55

1 Answer 1

1

You have to use onPress not onClick

<Button onPress={() => { this.setState({color: "#00ff00" }) }} title="selam"></Button>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it works but i need to click the button like 8-10 times for the change why?
i tested and works as expected,maybe your emulator is slow or something it takes time to update
Yes it was slow now its okey. Thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.