1

I have an unusual case. Let say, I have a form. When the user clicks the "save" button I need to trigger a component's method to convert draft.js content to HTML. Normally I just use refs to get access to child and call any method. But in my case draft component is inside render method of react-router v4 and appears only when URL match pattern. This is a hindrance cuz when I define ref

<Match pattern={'/info'} render={(props)=> <Draft_Editor ref='editor' />} />

parent component doesn't have this 'editor' in refs. What can i do to call method on from parent ?

2 Answers 2

1

One way is as follows.

1) Find the DOM node for the parent element using:

var parentDOM = ReactDOM.findDOMNode(parentCompInstance);

2) Find the DOM node for the child element, using:

var childDOM = parentDOM.children[0]; // or something like this, depending on html hierarchy

3) Find the React component instance for that child DOM node, using the solution mentioned here: React - get React component from a child DOM element?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I didn't know about such an ability of react
0

I found solution for my case. Unfortunate, the solution is using ref but it performs it with other approach. Instead of making this ref='editor' I did this ref={(draft) => this.aboutEditor = draft}. This editor will be available as this.aboutEditor. When i operated with ref as string it did't works. But it works fine as callback function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.