0

I have this js:

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import './index.css';

class Titulo extends Component{
    render(){
        return (
            <h1>Hola Mundo</h1>
        );
    }
}

class Contenido extends Component{
    constructor(props){
        super(props);
        this.state = {
            element: props.myDiv
        }
    }

    render(){
         return (
            <div ref={this.state.element} className="contenido">
                <p className="parrafo">
                Este es el contenido principal de la página
                </p>
            </div>
         );
    }
}

class LeftPanel extends Component{

constructor(props) {
    super(props);
    this.state = {
        daemon: props.textContent,
        showPopup: false,
        elem: 0
    };
}

togglePopup() {
    this.setState({
    showPopup: !this.state.showPopup
    });
}

addText(i){
    var t = <p id={i}>Texto agregado</p>
    var a = this.state.daemon;
    a.appendChild(t);
    this.setState({
        elem: i+1
    });
}

removeText(i){
    var a = this.state.daemon;
    a.removeChild(a.lastChild());
    this.setState({
        elem: i-1
    });
}

render(){
    return (
        <div className="left">
            <span className="spanLeft" onClick={this.togglePopup.bind(this)}>Popup</span><br/>
            <span className="spanLeft" onClick={this.addText(this.state.elem)}>Agregar text</span><br/>
            <span className="spanLeft" onClick={this.removeText(this.state.elem)}>Quitar texto</span><br/>
            <span className="spanLeft" onClick="">Elemento 4</span>                    
            {this.state.showPopup ? 
            <MyPopUp
                closePopup={this.togglePopup.bind(this)}
            />
            : null
            }
        </div>
    );
}   
}

class PiePagina extends Component{
render(){
    return (
        <div className="pie">
            <span>Pie de página</span>
        </div>
    );
}
}


class Cabecera extends Component{
constructor(props){
    super(props);
    this.mycontent = React.createRef();
}

render(){
    return (
        <div className="cuerpo">
            <div className="cabecera">
                <Titulo/>
            </div>
            <div className="row">
                <LeftPanel textContent={this.mycontent}/>
                <Contenido myDiv={this.mycontent}/>
            </div>
            <div>
                <PiePagina/>
            </div>
        </div>
    );
}
}

class PopUpContent extends Component{
render(){
    return(
        <div className="popupContent">
            <div>
                < span className="CloseButton"
                    onClick={this.props.closePopup} >
                    X
                </span>
            </div>
            <div >
                <span className="pie" >
                    Hola!Soy un popup     
                </span>
            </div> 
            <div className="parrafo" >
                <p>
                Este es un mensaje más que está dentro del popup 
                </p> 
            </div>
        </div>
    );
}
}

class MyPopUp extends Component{
render(){
    return(
        <div className="popup">
            <div className="popup_inner">
                <PopUpContent
                    closePopup={this.props.closePopup}
                />
            </div>
        </div>
    );
}
}
ReactDOM.render(<Cabecera/>,document.getElementById('root')); 

Ok, so I wish to access the div on class "Contenido" so I can add some text and delete some text after clicking on some elements.

But I get an error that says that a.appendChild() is not a method.

I see that it isn't being recognised as a method because when trying to acces it with this.state.daemon it isn't returning an element

Fernando Bravo Diaz
  • 551
  • 1
  • 4
  • 11
  • `appendChild()` is a function for html tags, not for React components. Is it your first React contact? – Yago Azedias Apr 11 '18 at 22:35
  • 1
    It seems you have not yet understood the underlying concept of React. In React, you don't directly manipulate the DOM, but rather your components may dispatch actions, which affect data properties which are then redistributed top down throughout your component tree, resulting in the update of the components in order to reflect the new prop contents. I recommend you consult one of the many extensive tutorials available. – GrafWampula Apr 11 '18 at 22:35
  • The property `this.state.daemon` is a value of you component state, that receive `props.textContent` from its component's father. – Yago Azedias Apr 11 '18 at 22:37
  • To answer @YagoAzedias question, yes, this is my first contact with react. So i don't have any experience about how does jsx work or how is it that one can manipulate the elements of each component. – Fernando Bravo Diaz Apr 11 '18 at 22:42
  • 1
    I recommend you to read the react's tutorial: https://reactjs.org/tutorial/tutorial.html There is lot of explanations about what is react, what is the JSX and how can you use the react environment correctly . – Yago Azedias Apr 11 '18 at 22:45
  • You should not manipulate DOM elements yourself. The react way is to alter the state of your data. Your component will reflect changes to the data by re-rendering. – trixn Apr 11 '18 at 22:45
  • Possible duplicate of [How to access a DOM element in React? What is the equilvalent of document.getElementById() in React](https://stackoverflow.com/questions/38093760/how-to-access-a-dom-element-in-react-what-is-the-equilvalent-of-document-getele) – Yago Azedias Apr 12 '18 at 13:10
  • Ok, after revising the react tutorial provided by @YagoAzedias i show some text after toggling a boolean value. Thanks everybody for your help. – Fernando Bravo Diaz Apr 13 '18 at 20:39

0 Answers0