0

So I'm a bit of a beginner to react native, and I'm making an app. I'm using react navigation to get a bottom navigation bar, but I want to make it so that within one of my pages, the user can press a button to change a component. Check out this quick mock-up I made:mockup, part 1

mockup, part 2

Basically, I want to make it so that when the user hits button 1, component 1 appears, but when the user hits button 2, component 2 appears instead. How do I show/hide components? Also how would I implement the buttons?

I know this question sounds rather basic but I really couldn't find any kind of tutorial on it. If you have the answer or can at least point me in the direction of a video or blog post that would answer my question, thank you very much!

1

1 Answer 1

1

Check the snippet here. Maintain state for selected nav time (button click), Then do conditional render based selected content type.

const actions = [
  { name: "button 1", type: "b1" },
  { name: "button 2", type: "b2" },
];

const Component = () => {
  const [contentType, setContentType] = React.useState("b1");

  const Content1 = () => <div> Content1 here </div>;
  const Content2 = () => <div> Content2 here </div>;

  return (
    <div>
      <div>
        {actions.map((action) => (
          <button
            key={action.type}
            style={{
              backgroundColor:
                action.type === contentType ? "lightblue" : "white",
            }}
            onClick={() => setContentType(action.type)}
          >
            {action.name}
          </button>
        ))}
      </div>
      <div style={{ marginTop: 20 }}>
        {contentType === "b1" && <Content1 />}
        {contentType === "b2" && <Content2 />}
      </div>
    </div>
  );
};

ReactDOM.render(<Component />, document.getElementById("app"))
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>


<div id="app"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I'll have to try this out to make sure it's exactly what I'm looking for, but 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.