score:1
Accepted answer
Here is an outline of how you can hide the buttons and show the relevant Component while specifying only one handleClick
method: http://codepen.io/PiotrBerebecki/pen/BLGmvd
const ParentComponentStyle = {
width:300,
height:60,
backgroundColor:"#343458"
};
class ParentCompnent extends React.Component {
constructor(props) {
super(props);
this.state = {
buttonPressedA: false,
buttonPressedB: false
};
this.handleClick = this.handleClick.bind(this);
}
handleClick(evt) {
this.setState({
[`buttonPressed${evt.target.id}`]: !this.state[`buttonPressed${evt.target.id}`]
});
}
render() {
let renderContent;
if (this.state.buttonPressedA) {
renderContent = (
<DivA>
<p>Child 0A</p>
<p>Child 1A</p>
<p>Child 2A</p>
<p>Child 3A</p>
<p>Child 4A</p>
</DivA>
);
} else if (this.state.buttonPressedB) {
renderContent = (
<DivB>
<p>Child 0B</p>
<p>Child 1B</p>
<p>Child 2B</p>
<p>Child 3B</p>
<p>Child 4B</p>
</DivB>
);
} else {
renderContent = (
<div className="DivToHide" style={ParentComponentStyle}>
<button onClick={this.handleClick} id="A">
Replace me with DivA
</button>
<button onClick={this.handleClick} id="B">
Replace me with DivB
</button>
</div>
);
}
return(
<div>
{renderContent}
</div>
);
}
}
const DivStyleA = {
width:300,
height:200,
backgroundColor:"green"
};
const DivA = React.createClass({
render: function() {
return(
<div style={DivStyleA}>
DivA
<div style={{float:'left'}}>{this.props.children[0]}</div>
<p style={{float:"left", color:"white",marginTop: "7px"}}>{this.props.children[1]}</p>
<div style={{float:"right"}}>{this.props.children[2]}</div>
<p style={{float:"right",color:"darkGray",paddingRight: "46px", marginTop: "0",fontSize:'12px', marginBottom:"0px"}}>{this.props.children[3]}</p>
<div style={{textAlign:"center"}}>{this.props.children[4]}</div>
</div>
);
}
});
const DivStyleB = {
width:300,
height:200,
backgroundColor:"red"
};
const DivB = React.createClass({
render: function() {
return(
<div style={DivStyleB}>
DivB
<div style={{float:'left'}}>{this.props.children[0]}</div>
<p style={{float:"left", color:"white",marginTop: "7px"}}>{this.props.children[1]}</p>
<div style={{float:"right"}}>{this.props.children[2]}</div>
<p style={{float:"right",color:"darkGray",paddingRight: "46px", marginTop: "0",fontSize:'12px', marginBottom:"0px"}}>{this.props.children[3]}</p>
<div style={{textAlign:"center"}}>{this.props.children[4]}</div>
</div>
);
}
});
ReactDOM.render(
<ParentCompnent />,
document.getElementById('app')
);
Source: stackoverflow.com
Related Query
- How to hide a parent div and replace it with another div when user clicks on a button that is a children of the parent div?
- How to hide a div and replace it with another div in reactjs?
- How to hide a child element as default and show it when users over over a parent element with material ui(react.js)?
- How to optimize React components with React.memo and useCallback when callbacks are changing state in the parent
- How do I hide an svg and show another when using a phone?
- Hide div with buttons when user uses a touch screen in a functional react component
- How to dispatch an action when another becomes fulfilled with reduxjs/toolkit React and Typescript?
- Track how many times a user clicks a button and later display it with React and Firebase
- How to distinguish admin and customer role when create user with createUserWithEmailAndPassword firebase auth in ReactJS
- How to replace a string with another and with a different colour in ReactJS?
- How to use useRef() for multiple components and to scroll down to a specific component when user clicks
- how to append react div element from an array with a child element ? and how to give to mapped parent elements unique "key" props
- how to hide one div when another div is active in react js
- How to replace a React component with another one when its state is changed
- How to Show and Hide with id when the checkbox is checked
- How to achieve an async function chain with await when the functions are parent and child?
- How to open pop up modal when form submit sucessfully and hide pop after 2 mintues and redirect to another page in react js?
- How to add a CSS class when page loads and remove it when user clicks on item
- How to achieve 2 divs with aspect ratio and stretch the 2nd div by the remaining height of the parent container
- How do I check all mui table checkboxes when the 'selectAll' checkbox is in a parent or sibling component and the table in another component
- Show one div and hide another in an array with onMouseEnter in React
- How to display the last page when user clicks on add new row,so that user can view the newly added row using react js and prime react
- How can I redirect user to another page when he clicks on alert box in Reactjs
- How not show the web application pages when the user is not logged in and he try to access the pages with just URI in Reactjs
- Detecting when user scrolls to bottom of div with React js
- Testing with React's Jest and Enzyme when simulated clicks call a function that calls a promise
- how to hide and show a div in react
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- How to setup self-closing when I save code on VSCode with prettier and ESLint?
- How to replace a component with another one upon event (button click) in react js
More Query from same tag
- Connect to an available SMTP sever using node.js
- Hide comma if the following word isn't present in React
- How do I fetch data in a React custom hook only once?
- How to overwrite default props of a higher order component?
- Attempt to render state value from ajax call in componentDidMount failing (React)
- React Formik use submitForm outside <Formik />
- Image gallery with Ionic React using Ionic-Swipe
- how to change path with Link, NavLink, or Redirect (not full reload) in input navbar react
- Webpack Dev Server cannot find modules with Typescript if I use custom paths in tsconfig
- How can I limit the input tag to only 10 Commas?
- How to add font ttf file in nextjs
- Iterating over object that contains objects to use result
- How to print json object array to print all the values as category and subcategory
- react warning cannot set state when using promises
- Getting 403 Access Denied Errors When Hosting a React Router App in AWS S3 and CloudFront
- How to change sequelize model.findOne() target table?
- How do I return data from multiple APIs to display on one page in React?
- React update view when api's loading variable is false
- custom widget was not showing in mozilla-services-react-jsonschema-form
- React Navbar issue with link locaton
- How to create object id:name format in Reactjs
- Anonymous class in React
- Alter state of parent from a child component React js
- When trying access event.target.$something always return 'undefined'
- history.push in react router 4
- how to make variables inside useEffect accessible from outside?
- Axios post does not send data
- Display label if list is empty
- Append to array in async for loop
- state undefined input form react