1

I've two React components, I want a button click in one to replace it entirely with the second.

So my first Component is delivered inside App root,

function App(){
    
  return (
    <div>
      <Landing />
    </div>
  )
}

export default App;

Inside landing, I want a button click to replace it in App with a different Component Marketing. This is not a child Component and they're at the same level.

I'm new to React and have no idea how to do this. Any help appreciated.

8
  • You probably want to "lift state," a React concept in which state that is required by one or more components be lifted to a common ancestor. In this case, App would maintain some state for toggling between the two components, and pass a function for setting it down to <Landing/>. Commented May 3, 2022 at 3:29
  • How do I pass the state change up from child to parent? Commented May 3, 2022 at 4:02
  • You can pass state + it's setter function down to child component, and using setter to change parent state value, and that'll "pass state up to parent" Commented May 3, 2022 at 4:04
  • 1
    @TannishaHill - Here is a JSFiddle example showing how this could be accomplished using lifting state. That said, if you are using this to do a lot of changing of views, depending on the context you might be better off leveraging a router like React Router, which is intended to manage top level "views" or "pages" in your app and tie them to URLs. Good luck, and happy coding! Commented May 3, 2022 at 13:18
  • export that button into separate file and import where ever you want. does that work ? Commented May 3, 2022 at 13:24

2 Answers 2

2

You can achieve this by using conditional rendering. You want to create a boolean var in App (myBool) and a function (toggleBool) that flips that var. Then you can pass this function as a prop to each component to use as an event for your button. Then you can conditional render these components in App.

import { useState } from "react";
import "./styles.css";

function App() {
  const [myBool, setmyBool] = useState(true);

  function toggleBool() {
    setmyBool(!myBool)
  }

  return (
    myBool ? <Landing toggleBool={toggleBool} /> : <Marketing /> 
  );
}

function Landing(props){
  return (
    <div>
      <h1>Landing</h1>
      <button onClick={props.toggleBool}>Button</button>
    </div>
  )
}

function Marketing(props){
  return (
    <h1>Marketing</h1>
  )
}

If you want to be able to toggle back to Landing from Marketing, you can pass the toggleBool function into Marketing like how its done to Landing and use the button the same way.

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

4 Comments

The button is inside the landing component.
Yes, since both the Landing and Marketing component are in App component, you can create a function in App that will flip the bool ( myBool = !myBool) and pass this function as a prop into Landing component. Then this function can be used as an onclick event for you button in Landing.
Can you please provide some code example.
I added an example. You can copy and paste this into codesandbox to see it in action.
0

I've used something like this for showing a loading screen while fetching data from the database. I've also used something similar to this for changing themes with a button.

// create pressed state
const [pressed, setPressed] = useState(false);

// onClick() function
function press() {
    if (pressed == false) setPressed(true) else setPressed(false)
}

// true or false // change component
const component = pressed ? <NotLanding onClick(press) /> : <Landing onClick(press) />

function App() {
    return (
        <div>
            {component} {/* component that will switch onClick() */}
        </div>
    )
}

2 Comments

The button is inside the landing component.
Without a separate file containing the component and a function inside of such file that does the exact thing, its not possible.

Your Answer

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