Skip to content

Instantly share code, notes, and snippets.

@swashcap
Created September 19, 2018 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swashcap/0ca52038786cc813407540c76845a4c7 to your computer and use it in GitHub Desktop.
Save swashcap/0ca52038786cc813407540c76845a4c7 to your computer and use it in GitHub Desktop.
// Dynamic state example
import * as React from 'react';
import * as ReactNative from 'react-native';
import connect from 'react-redux'
class MyComponent extends React.Component {
constructor(props) {
// Assign state based on props
// A contrived example:
if (props.count > 0) {
this.state = {
count: props.count,
hasCount: true
}
} else {
this.state = {
count: 0,
hasCount: false
}
}
}
render() {
return (
<ReactNative.View>
{this.state.hasCount &&
<ReactNative.Text>{this.state.count}</ReactNative.Text>
}
</ReactNative.View>
);
}
}
export default connect(
({ count }) => count
)(MyComponent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment