0

I have two components App component and the Component2

import Component2 from './Component2';
  
class App extends Component {  
  
    state={data:""}
  
    changeState = () => {  
      this.setState({data:`state/props of parent component 
      is send by onClick event to another component`}); 
         }; 
  
    render(){   
        return (     
            <div className="App">  
                <Component2 data={this.state.data} />   
                <div className="main-cointainer">
                    <h2>Compnent1</h2> 
          <button  onClick={this.changeState} type="button"> 
             Send state 
          </button>    
                </div>
            </div>   
        );          
    }}
  
    export default App;

and the Component2.js

const Component2 = (props) => {
    return (
        <div className="main-cointainer">
            <h2>Compnent2</h2> 
              
<p>{props.data} </p>
  
        </div>
    )
}
  
export default Component2;

Here I am passing the data from App component to the Component2 component . What I want is to hide the Component2 from being rendered while passing the data from App component to the Component2 component .

I tried writing this statement

{ false && <Component2 data={this.state.data} /> }

but this did not work out .

Any suggestions to achieve this type of requirement ?

3
  • why you starting a conditional rendering with a falsy boolean? Have you read the related section in docs? reactjs.org/docs/… Commented Apr 12, 2021 at 13:41
  • Do you mean to say that you don't want to show component2 when you have some data in the data property? Commented Apr 12, 2021 at 13:47
  • @Dennis because I dont want to render Component2 in my App that is my requirement . Commented Apr 12, 2021 at 14:02

3 Answers 3

1

To hide Component2 (without changing its functionality), add it in a div with style = {{display: 'none'}}

App.js

import React from "react";
import "./style.css";

import Component2 from "./Component2";

export default function App() {
  return (
    <div>
      <h1>Hello StackBlitz!</h1>
      <p>Start editing to see some magic happen :)</p>
      <div style={{ display: "none" }}>
        <Component2 myProps="this is a example" />
      </div>
    </div>
  );
}
  

Check demo : https://stackblitz.com/edit/react-bbxgcr?file=src%2FApp.js

PS : Open the console to see the props in correctly passed to Component2

4
  • I made a mistake, it must be done for Component2 (see my edited answer)
    – MB_
    Commented Apr 12, 2021 at 13:45
  • I dont want to change functionality of component2 . Commented Apr 12, 2021 at 13:56
  • @RajGopalbh4 Try my new solution
    – MB_
    Commented Apr 12, 2021 at 14:07
  • Thank you . your new solution is working well . Commented Apr 12, 2021 at 14:19
0

you can do this with only one if. If I understood the problem correctly

const Component2 = (props) => {
 if(props.data === ""){
 return(<div></div>)
}
    return (
        <div className="main-cointainer">
            <h2>Compnent2</h2> 
              
<p>{props.data} </p>
  
        </div>
    )
}
  
export default Component2;
1
  • I dont want to change functionality of component2 Commented Apr 12, 2021 at 14:04
0

Try using negation to simply convert an empty string into a boolean value.

 { !!this.stat.data && <Component2 data={this.state.data} /> }  

When the this.stat.data is an empty string or falsy value then the Component2 will not render

Or

If you don't want to show display component on the page try this

<Component2 data={this.state.data} hidden={true}/>

Component2

const Component2 =({ data, hidden }) => {
  return (
    <div hidden={hidden}>
      <h1>Component2</h1>
    </div>
  );
}

Adding hidden attribute on the HTML tag will hide the element on the page.

1
  • I always don't want to show Component2 irrespective of what ever might be the content of data . Commented Apr 12, 2021 at 13:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.