Access parent's dom node from child component react react


Alessandro

I have a grid component like this:

import React, { Component } from 'react';

import Action from './action.jsx';

class Grid extends Component {

    constructor(props) {
        super(props);
        this.maxN = 110;
        this.tempArray = [];
        this.question;
    }

    getRandomN() {
        var randomN = Math.floor((Math.random() * this.maxN) + 1);

        if(this.tempArray.indexOf(randomN) === -1) {
            this.tempArray.push(randomN);
        }
        else {
            randomN = this.getRandomN();
        }

        return randomN;
    }

    getRandomQuestion() {   
        this.question = this.props.current.data.questions[this.getRandomN()];
        return this.question;
    }

    render() {
        this.getRandomQuestion();

        return (
            <section className="game">
                <div className="grid">

                    <div className="row">
                        <div ref="n1"></div>
                        <div ref="n2"></div>
                        <div ref="n3"></div>
                        <div ref="n4"></div>
                        <div ref="n5"></div>
                        <div ref="n6"></div>
                    </div>

                    <div className="row">
                        <div ref="n7"></div>
                        <div ref="n8"></div>
                        <div ref="n9"></div>
                        <div ref="n10"></div>
                        <div ref="n11"></div>
                        <div ref="n12"></div>
                    </div>

                    <div className="row">
                        <div ref="n13"></div>
                        <div ref="n14"></div>
                        <div ref="n15"></div>
                        <div ref="n16"></div>
                        <div ref="n17"></div>
                        <div ref="n18"></div>
                    </div>

                    <div className="row">
                        <div ref="n19"></div>
                        <div ref="n20"></div>
                        <div ref="n21"></div>
                        <div ref="n22"></div>
                        <div ref="n23"></div>
                        <div ref="n24"></div>
                    </div> 
                </div>

                <Action question={this.question} getRandomQuestion={this.getRandomQuestion.bind(this)}/>
            </section>
        );
    }
}

export default Grid;

In the "action" component, based on the correct or incorrect answer from "getNewQuestion", I need to access random grid elements from the grid component. (any random number from "n1" to "n24" assigned to each ref attribute)

import React, { Component } from 'react';

class Action extends Component {
    constructor(props) {
        super(props);

        this.state = {
            question: props.question
        }
    }

    getNewQuestion(e) {
        console.log(this.state.question.correct_option);

        let answerId = "option_" + this.state.question.correct_option;

        if(e.target.getAttribute('data-question') == answerId) {
            this.setState({
                question: this.props.getRandomQuestion()
            });
        }
        else {
            console.log('wrong');

            React.findDOMNode(this.refs.n1).classList.add('fdsdfsdfsdfsdfsfsdf');
        }
    }

    render() {
        let state = this.state;

        return(
            <div className="action">
                <div className="action-question">
                    <h3>{state.question.question}</h3>
                </div>

                <div className="action-answers">
                    <p data-question="option_1" onClick={this.getNewQuestion.bind(this)}>{state.question.option_1}</p>
                    <p data-question="option_2" onClick={this.getNewQuestion.bind(this)}>{state.question.option_2}</p>
                    <p data-question="option_3" onClick={this.getNewQuestion.bind(this)}>{state.question.option_3}</p>
                    <p data-question="option_4" onClick={this.getNewQuestion.bind(this)}>{state.question.option_4}</p>
                </div>
            </div>
        );
    }
}   

export default Action;

In the "if" statement of "getNewQuestion" I want to do the following:

n2.classList.addClass('hidden');

I don't know how to access the parent's dom node from the "action" component

Jonah River

Does the child really need direct access to the parent DOM? Shouldn't the parent component know how to present itself? If so, you can use a callback passed to the child so that the child has the possibility to notify the parent when the parent should be changed.

const Child = ({modifyParent}) => (
  <div onClick={ modifyParent } >Click me!</div>
);

const Parent = () => {
  const modifyMyOwnStyle = event => {
    // here you have easy access 
    // if you want to style the parent.
    alert('Modifying style, based on child call');
  }

  return (
    <Child modifyParent={ modifyMyOwnStyle }/>
  );
};

ReactDOM.render(<Parent />, document.getElementById('root'));

A working JSFiddle demo is here

Related


Access parent's dom node from child component react react

Alessandro I have a grid component like this: import React, { Component } from 'react'; import Action from './action.jsx'; class Grid extends Component { constructor(props) { super(props); this.maxN = 110; this.tempArray = [];

Access parent's dom node from child component react react

Alessandro I have a grid component like this: import React, { Component } from 'react'; import Action from './action.jsx'; class Grid extends Component { constructor(props) { super(props); this.maxN = 110; this.tempArray = [];

Access parent's dom node from child component react react

Alessandro I have a grid component like this: import React, { Component } from 'react'; import Action from './action.jsx'; class Grid extends Component { constructor(props) { super(props); this.maxN = 110; this.tempArray = [];

Access parent's dom node from child component react react

Alessandro I have a grid component like this: import React, { Component } from 'react'; import Action from './action.jsx'; class Grid extends Component { constructor(props) { super(props); this.maxN = 110; this.tempArray = [];

Access parent's dom node from child component react react

Alessandro I have a grid component like this: import React, { Component } from 'react'; import Action from './action.jsx'; class Grid extends Component { constructor(props) { super(props); this.maxN = 110; this.tempArray = [];

Render child into parent component's DOM node using React Portal

Egor Egorov What I'm trying to do seems a little out of the way, so please bear with me. I want to render a child component into a parent component's DOM node using React Portal. You might be thinking: "That's not what portals are for, you should put your chil

Render child into parent component's DOM node using React Portal

Egor Egorov What I'm trying to do seems a little out of the way, so please bear with me. I want to render a child component into a parent component's DOM node using React Portal. You might be thinking: "That's not what portals are for, you should put your chil

remove child component from parent dom node in react

WitVault Unmount components from the same DOM element that mounts them. ReactDOM.render(<SampleComponent />, document.getElementById('container')); Then, we uninstall it: React.unmountComponentAtNode(document.getElementById('container')); How to detach and r

remove child component from parent dom node in react

WitVault Unmount components from the same DOM element that mounts them. ReactDOM.render(<SampleComponent />, document.getElementById('container')); Then, we uninstall it: React.unmountComponentAtNode(document.getElementById('container')); How to detach and r

remove child component from parent dom node in react

WitVault Unmount components from the same DOM element that mounts them. ReactDOM.render(<SampleComponent />, document.getElementById('container')); Then, we uninstall it: React.unmountComponentAtNode(document.getElementById('container')); How to detach and r

remove child component from parent dom node in react

WitVault Unmount components from the same DOM element that mounts them. ReactDOM.render(<SampleComponent />, document.getElementById('container')); Then, we uninstall it: React.unmountComponentAtNode(document.getElementById('container')); How to detach and r

remove child component from parent dom node in react

WitVault Unmount components from the same DOM element that mounts them. ReactDOM.render(<SampleComponent />, document.getElementById('container')); Then, we uninstall it: React.unmountComponentAtNode(document.getElementById('container')); How to detach and r

Access child state of child from parent component in react

June I want to access state <Child2>from parent component . <Parent> <Child1> <Child2> </Child1> </Parent> How to achieve? Trickson You shouldn't be doing this at all. In a reaction, information should always flow from top to bottom. The reactive

Access child state of child from parent component in react

June I want to access state <Child2>from parent component . <Parent> <Child1> <Child2> </Child1> </Parent> How to achieve? Trickson You shouldn't be doing this at all. In a reaction, information should always flow from top to bottom. The reactive

Access child state of child from parent component in react

June I want to access state <Child2>from parent component . <Parent> <Child1> <Child2> </Child1> </Parent> How to achieve? Trickson You shouldn't be doing this at all. In a reaction, information should always flow from top to bottom. The reactive

Access child state of child from parent component in react

June I want to access state <Child2>from parent component . <Parent> <Child1> <Child2> </Child1> </Parent> How to achieve? Trickson You shouldn't be doing this at all. In a reaction, information should always flow from top to bottom. The reactive

Access child state of child from parent component in react

June I want to access state <Child2>from parent component . <Parent> <Child1> <Child2> </Child1> </Parent> How to achieve? Trickson You shouldn't be doing this at all. In a reaction, information should always flow from top to bottom. The reactive

Access child state of child from parent component in react

June I want to access state <Child2>from parent component . <Parent> <Child1> <Child2> </Child1> </Parent> How to achieve? Trickson You shouldn't be doing this at all. In a reaction, information should always flow from top to bottom. The reactive

Access child state of child from parent component in react

June I want to access state <Child2>from parent component . <Parent> <Child1> <Child2> </Child1> </Parent> How to achieve? Trickson You shouldn't be doing this at all. In a reaction, information should always flow from top to bottom. The reactive