2
I would like to know how to destroy a component with React by clicking a button. I found a way out, but in my conception it is quite "gambiarrosa". I have a state called show
and when click on close change the value of this.state.show
to false, in the render() method I use a ternary if to render the component or not depending on the value this.state.show
.
Mycomponent.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import FontAwesome from 'react-fontawesome';
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
show:true,
};
}
closeComponent(){
console.log("Destruir componente");
var x = ReactDOM.findDOMNode(this);
console.log(x);
//ReactDOM.unmountComponentAtNode(this.container);
this.setState({ show: false });
}
componentDidMount() {
}
componentWillUnmount() {
clearInterval(this.state);
}
render() {
return (
this.state.show ?
<div className="MyComponent">
<h1>Mostrar componente</h1>
<FontAwesome name="window-close-o" onClick={this.closeComponent.bind(this)}/>
</div>
: null
);
}
}
export default MyComponent;
This is really the only way out for what I need to do?
Componentfather.js
import React, { Component } from 'react';
import './ComponentFather.css';
import MyComponent from '../MyComponent/MyComponent'
class ComponentFather extends Component {
constructor(props) {
super(props);
this.state = {
value: "Adicionar Categoria",
key: 0,
matchs: [],
};
}
componentDidMount(){
}
componentWillUnmount(){
clearInterval(this.state);
}
buttonClicked(){
var mat = this.state.matchs;
this.state.key ++;
mat.push(<Match key={this.state.key}/>);
console.log(mat);
this.setState({
matchs: mat
});
}
render() {
return (
<div className="ComponentFather">
<h1>Events Information</h1>
<MyComponent></MyComponent>
<MyComponent></MyComponent>
<MyComponent></MyComponent>
</div>
);
}
}
export default ComponentFather;
Maincomponent
import React, { Component } from 'react';
//import logo from './logo.svg';
import './App.css';
import ComponentFather from '../ComponentFather/ComponentFather';
import Streaming from '../Streaming/Streaming';
//import {Grid, Row, Col, Clearfix} from 'react-bootstrap';
class App extends Component {
render() {
return (
<div className="App">
<ComponentFather />
<Streaming />
</div>
);
}
}
export default App;
index js.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App/App';
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap/dist/css/bootstrap-theme.css'
ReactDOM.render(
<App />,
document.getElementById('main-container')
);
I just saw your Dit. Give me an idea that your code is adapted and not the real code. Two questions: These
MyComponent
are always 3 or is it dynamic? Another question, theComponentFather
knows the state of eachMyComponent
?– Sergio
I’ve got all three tied up just to have a representation of what it’s gonna be like, but the right thing is to be dynamic. As for your other question, I don’t know the answer. I started the studies in React these days, devouring the documentation to see if it clears things up more. The idea is this, the Componentfather will load a list of objects from the backend and for each object in the list I will create a Mycomponent passing the object as parameter, the properties of the object will all be displayed in Mycomponent and within Mycomponent I will have the option to destroy that component by clicking on a given button....
– LuKs Sys
Okay. If the
ComponentFather
keep the state of eachMyComponent
he will know whether or not to show a givenMyComponent
. This seems to me the right way. Are you using Flux? Mobx? None of them? Soon I will give an example to see if this is what you are looking for– Sergio
For now I am only in the static part but I want to use Flux, so I have read the most viable. But I have to dig a little deeper because the communication will be via socket. From what I saw in your answer that’s exactly what I need. Thank you so much for the strength you’re giving me. Just one more thing... Is there any material you recommend to boost my learning around React?
– LuKs Sys
Take a look here however: https://www.youtube.com/watch?v=Wsg3aSvPCXE
– Sergio
One more question for the example I’m setting: these
MyComponent
are a representation of what? an array? a BD list? keys of an object?– Sergio
Keys to an object
– LuKs Sys
I edited the answer, how you’re doing it works well. Depending on the logic of the parent element you can have an array with child elements not render via "parent", but as you have it works well. Take a look at my jsFiddle, it may be useful for testing.
– Sergio