简体   繁体   English

reactState vs refs in react.js

[英]setState vs refs in react.js

I created tabs in react and and now on click I have to change the class of the tabs the tabs classes may be as follows: 我在react中创建了选项卡,现在单击我必须更改选项卡类的选项卡类,如下所示:

1:active 1:有源
2:previousActive 2:previousActive
3:alreadySelected 3:alreadySelected

On click of a tab class become active and check whether it is selected before or not using alreadySelected class and active class from the last active tab is remove and if it is not alreadySelected then add alreadySelected . 在一个标签类的点击变得active ,并检查它是否选择使用前或不alreadySelected类和active类从最后一个活动标签是删除,如果它不alreadySelected再加入alreadySelected

Code of one tab in react: 响应中的一个选项卡的代码:

var TabBody = React.createClass({
    getInitialState: function() {
        return {
            class: 'tabBody tab activeTab'
        }
    },
    render: function() {
        a.tabBody = this;
        return (React.createElement('div', {
            className: this.state.class,
            ref: 'body',
            onClick: handleTabClick
        },
        React.createElement('span', {}, "Body"))
        );
      }
});

In order to change the class of the tabs I am doing in two ways and want to know which is effective. 为了改变我正在做的标签的类有两种方式,并想知道哪个是有效的。 Code style one: 代码风格一:

 var bodyClass = (a.tabBody.state.class).split(' ');
 var sleeveClass = (a.tabSleeve.state.class).split(' ');
 var neckClass = (a.tabNeck.state.class).split(' ');
 if (data === 'tabBody') {
     bodyClass.push('activeTab');
     var str1 = program.arrayToString(bodyClass);
     Interfaces.tabBody.setState({
         class: str1
     });
 }

Code Style 2 代码风格2

a.tabBody.refs.body.classList.remove('activeTab');
a.tabBody.refs.body.classList.add('tabPreviewComplete');
a.tabSleeve.refs.body.classList.add('activeTab');

Which style is good for doing this and why? 哪种风格有利于这样做以及为什么?

The point of react is that you do not need to/ should not update DOM directly . 反应的重点是您不需要/不应该直接更新DOM The idea behind react is that you render react components (virtual DOM), and that you let react figure out if and how to update DOM. 反应背后的想法是你渲染反应组件(虚拟DOM),并让你反应出来是否以及如何更新DOM。

Changing classes using refs is a very risky strategy: Your component's state is then no longer in sync with actual DOM, which could bring you into debugging nightmares later on. 使用refs更改类是一种非常冒险的策略:您的组件的状态不再与实际DOM同步,这可能会让您在以后调试噩梦。 So I would pose that Code Style 2 (even though it works) violates react principles. 所以我认为Code Style 2(尽管它有效)违反了反应原则。 One of the few exceptions for using refs, is to add a listener to a DOM component after it is mounted. 使用refs的少数例外之一是在安装DOM组件后添加一个侦听器。

The react way is to put the classNames in state. 反应方式是将classNames置于状态。
And do a setState() to update. 并执行setState()更新。
And let react do the DOM update, 让反应做DOM更新,
which is very likely to be way faster, cleaner, and easier to maintain than getting refs, and changing classNames. 这比获取引用和更改classNames更有可能更快,更清晰,更容易维护。

ref means you are using the actual DOM and setState means you are saying to react that please update the specific attribute of the component. ref意味着你正在使用实际的DOM和setState意味着你要做出反应,请更新组件的特定属性。 every thing is maintain by react. 每件事都是通过反应来维持的。

On the other hand if you use refs it means you are doing every thing your own and react have no concern to your attributes and properties you are updating. 另一方面,如果您使用refs,则意味着您正在做自己的所有事情并且反应与您正在更新的属性和属性无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM