1

I have a React Native component that consists of 3 buttons components, each one has a boolean property called inner. If inner={true} then the button appears pressed in, whereas if inner={false} then the button appears embossed.

Only one button can be active at any one time, I'd like to make it such that the active button will display inner={true} and the other two inactive buttons will display inner={false}.

Here is my code below:

const ChoiceContainer = props => {
  const {children} = props;

  return <View>{children}</View>;
};

const SendTransaction = () => (
  <ChoiceContainer>
    <Text>Component 1</Text>
  </ChoiceContainer>
);

const Remove = () => (
  <ChoiceContainer>
    <Text>Component 2</Text>
  </ChoiceContainer>
);

const History = () => (
  <ChoiceContainer>
    <Text>Component 3</Text>
  </ChoiceContainer>
);

export default class MyComponent extends Component {
  state = {
    sceneType: 'add',
  };

  showSend = () => {
    this.setState({sceneType: 'add'});
  };

  showReceive = () => {
    this.setState({sceneType: 'remove'});
  };

  showHistory = () => {
    this.setState({sceneType: 'history'});
  };

  renderScene = type => {
    if (type === 'add') {
      return <SendTransaction />;
    }

    if (type === 'remove') {
      return <ReceiveTransaction />;
    }

    if (type === 'history') {
      return <TransactionHistory />;
    }
  };

  render() {
    const {sceneType} = this.state;

    return (
      <View>
        <View>
          <TouchableOpacity onPress={this.showAdd}>
            <Button inner={true}>Button 1</Button>
          <TouchableOpacity onPress={this.showRemove}>
            <Button inner={false}>Button 2</Button>
          </TouchableOpacity>
          <TouchableOpacity onPress={this.showHistory}>
            <Button inner={false}>Button 3</Button>
          </TouchableOpacity>
        </View>
        <View>{this.renderScene(sceneType)}</View>
      </View>
    );
  }
}

1 Answer 1

2
     <TouchableOpacity onPress={this.showAdd}>
        <Button inner={sceneType === 'add' }>Button 1</Button>
      <TouchableOpacity onPress={this.showRemove}>
        <Button inner={sceneType === 'remove'}>Button 2</Button>
      </TouchableOpacity>
      <TouchableOpacity onPress={this.showHistory}>
        <Button inner={sceneType=== 'history'}>Button 3</Button>
      </TouchableOpacity>
Sign up to request clarification or add additional context in comments.

1 Comment

That worked - I did not realize I could pass conditions in like that. 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.