K
K
kirill3215922019-12-27 22:20:01
React
kirill321592, 2019-12-27 22:20:01

How to forward cnt from child component to parent in render react?

I am highlighting dom elements by search. The render of the highlighted elements was taken from the plugin (code below), it works fine, but now how to display their number in the parent component? I tried to forward the showCnt function through the parent callback, but it cannot be called in render, and in componentDidUpdate it works with a delay.

cnt:[],

render: function () {

    var p = this.props,
        highlightStart = p.string.search(p.highlight);

    if (!p.highlight || highlightStart === -1) {
        return h('span', null, p.string);
    }
    var highlightLength = p.highlight.source.length,
        highlightString = p.string.substr(highlightStart, highlightLength);

    this.cnt.push(highlightString);
    this.props.showCnt(this.cnt.lenght);
    return (
        h('span', null,
            p.string.split(p.highlight).map(function (part, index) {
                return h('span', { key: index },
                    index > 0 ?
                        h('span', { className: 'json-inspector__hl' }, highlightString) :
                        null,
                    part);
            }))
    )

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lev Zabudkin, 2019-12-28
@zabudkin

Yes, this is not a delay, it is, this is the mechanism of the machine itself. 0.5 sec Nothing below.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question