Is it possible to show/hide an element in a parent without rerendering?

Trip

Please correct me if my thought process differs from the orthodoxy of React.js..

I have a parent < MainViewController /> that has a <View /> child.

If a scrolling event happens in my <View />, then I want to show/hide a button in my parent < MainViewController />.

Unfortunately right now, I'm doing setState to toggle, but it rerenders parent, and consequentially, all of its children. Basically, it resets my entire app. Is there a simpler way to toggle hide/show without rerendering?

My relevant code :

var MainViewController = React.createClass({
  getInitialState () {
    return {
      showAskQuestion: false,
    };  
  },
  toggleFloatingButton () {
    this.setState({
      showAskQuestion: !this.state.showAskQuestion
    });
  },
    render () {
        return (
            <Container>
                <UI.NavigationBar name="main" />
        <UI.Button type="info" className={ this.state.showAskQuestion ? '' : 'hidden' } >
          Ask A Question
        </UI.Button>      
                <ViewManager name="main" defaultView="tabs">
                    <View name="tabs" component={TabViewController} toggleFloatingButton={this.toggleFloatingButton} />
                </ViewManager>
            </Container>
        );
    }
});
Dan Prince

The core idea of React is that when something changes in a component, it re-renders the entire component. To prevent this from being horribly slow, React only renders it as far as a virtual representation of the DOM, then checks the virtual DOM against the real one and makes the minimum number of changes to get them in sync.

When you update the state in your component, React will build up the virtual representation, but it'll be almost identical to the real DOM. The only difference being the following attribute:

className={ this.state.showAskQuestion ? '' : 'hidden'}

The only piece of "actual" re-rendering React will do, is updating this attribute.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

ShowHide Multiple li> blockquote #Quote

分類Dev

Select parent element in stylus

分類Dev

Is encapsulation possible without OOP?

分類Dev

ReplaceDocumentAsync without Document is that possible?

分類Dev

Overflowing a child element outside the parent element

分類Dev

Selector for the child element for the current parent element

分類Dev

float right element outside of parent

分類Dev

React is not changing the state of the parent element

分類Dev

XPath find parent of a child element

分類Dev

nested element firing parent event

分類Dev

put element on bottom of parent container

分類Dev

Element not filling parent in angular material

分類Dev

jquery find element considering parent

分類Dev

Tooltip hidden inside parent element

分類Dev

mathjax + vue not rerendering equations

分類Dev

Right alignment without resizing parent

分類Dev

Reload Parent Window without POST

分類Dev

Jenkins archival without parent directories

分類Dev

Is it possible to validate the root element in Schematron?

分類Dev

Possible to dispatch an action without connect?

分類Dev

Is it possible to bind to events on a child window from a parent?

分類Dev

Rerendering the same UI more times

分類Dev

React SVG not rerendering on attribute update

分類Dev

onclick to show <p><h1><iframe> , function showHide(shID)

分類Dev

React - expressions must have one parent element?

分類Dev

Check if cursor is inside element's parent

分類Dev

How to get the value of an element by traversing to parent div

分類Dev

Child elements expanding outside parent element with flex

分類Dev

Change color of SVG when hover on parent element

Related 関連記事

ホットタグ

アーカイブ