currently have a function that returns the following container. This container renders two buttons dynamically according to the user query. So far if I click one of the button’s, they both disappear, which is what I want.
class respondContainer extends React.PureComponent {
componentDidMount(){
var buttons = document.getElementsByClassName('myClass');
buttons.forEach(btn=>{
btn.addEventListener("click",(e)=>{
buttons.forEach(btn=>{
btn.style.display = "none";
})
})
})
}
render() {
return (
<responses
tabIndex={this.props.tabIndex}
replies={this.props.replies} //this is the array that contains the buttons to be rendered
sendMessage={this.props.sendMessage}
styles={this.props.styles}
/>
)
}
} However, I also want that once I click a button, they should both be deleted from the document’s elements so that when the app is reloaded, these buttons are not rendered from localStorage either
But the above case only makes the button I click on disappear and both of the buttons re-appear after the page is reloaded (both of which are behaviours I’m trying to avoid)
Hello, thanks for your reply! I used addEventListener because an answer I found online also made use of it. I’m still a little confused as to how to incorporate your answer for my container? I’ve commented the part which generates the buttons as well.
I don’t think I’ll be able to share the github sorry! Its a lot of components though so I understand that its hard to help. Is there any way we could just use this platform? I understand if its not possible though. This is the only class reponsible for rendering the buttons though
Hello @hola_world
I am wondering whether you are using react router. If you are not, then it is logical that navigating back to a “page” renders the component again hence losing the state in which it was initially. I have also noticed certain things in your code which are not reactish.
You are not naming components correctly. In react, component names must be capitalized like RespondContainer not respondContainer.
Just like @emma-r-slight pointed out, I don’t think you really need to use vanilla JS method for selecting your DOM elements. There are certain APIs built in react which can do that for you.