React-native: View: Style doesn't update with setState

Created on 23 Aug 2015  路  3Comments  路  Source: facebook/react-native

When I try to change the View style by changing the state. it will not respond. Do you have any idea why?

var React = require('react-native');

var { View, Text} = React;

var TouchIt = React.createClass({

  getInitialState() {
    return {
      style: style
    }
  },

  shouldSetResponder() {
    return true;
  },

  shouldSetMoveResponder() {
    return true;
  },

  responderGrant() {
    console.log('finger in');
  },

  responderMove() {
    var style = this.state.style;
    style.backgroundColor = this.getRandomColor();
    this.setState({backgroundColor: backgroundColor});
  },

  responderRelease() {
    console.log('finger out');
  },

  getRandomColor() {
    var randomColor = '#'+Math.random().toString(16).substr(-6);
    return randomColor;
  },

  render() {

    return (
        <View style={this.state.style}
          onStartShouldSetResponder={this.shouldSetResponder}
          onMoveShouldSetResponder={this.shouldSetMoveResponder}
          onResponderGrant={this.responderGrant}
          onResponderMove={this.responderMove}
          onResponderRelease={this.responderRelease}
          >
          <Text>{this.style.backgroundColor}</Text>
        </View>
    );
  }

});

var style = {
  backgroundColor: 'rgb(11, 135, 91)',
  flex: 1
}

module.exports = TouchIt;

Locked

Most helpful comment

Not sure if this will fix things but try changing the first line in responderMove to var style = {...this.state.style}; so that you create a new object and make it clear that the new View on the second render pass has a different style than the first one.

All 3 comments

Not sure if this will fix things but try changing the first line in responderMove to var style = {...this.state.style}; so that you create a new object and make it clear that the new View on the second render pass has a different style than the first one.

Not 100% sure as I have not tested it, but you seem to have two backgroundColors, one in your "var style" and the other in "this.state" and I think you are mixing them up. You should decide whether you want to have the backgroundColor saved here or there, and respectively set the correct value in your responderMove method.

Here you go!

var React = require('react-native');

var {
  AppRegistry,
  Image,
  ScrollView,
  StyleSheet,
  Text,
  View,
} = React;

var ExampleApp = React.createClass({

  getInitialState() {
    return {
      style: style
    }
  },

  shouldSetResponder() {
    return true;
  },

  shouldSetMoveResponder() {
    return true;
  },

  responderGrant() {
    console.log('finger in');
  },

  responderMove() {
    var backgroundColor = this.getRandomColor();
    this.setState({style: {backgroundColor}});
  },

  responderRelease() {
    console.log('finger out');
  },

  getRandomColor() {
    var randomColor = '#'+Math.random().toString(16).substr(-6);
    return randomColor;
  },

  render() {

    return (
        <View style={[{flex: 1}, this.state.style]}
          onStartShouldSetResponder={this.shouldSetResponder}
          onMoveShouldSetResponder={this.shouldSetMoveResponder}
          onResponderGrant={this.responderGrant}
          onResponderMove={this.responderMove}
          onResponderRelease={this.responderRelease}
          >
          <Text>{this.state.style.backgroundColor}</Text>
        </View>
    );
  }

});

var style = {
  backgroundColor: 'rgb(11, 135, 91)',
  flex: 1
}

Lots of issues with variables, check out my code above to see where you went wrong :) By the way, this kind of question is better suited for StackOverflow!

Was this page helpful?
0 / 5 - 0 ratings