简体   繁体   English

有没有一种在React.js中使用私有变量和方法的好方法

[英]Is there a good way to use private variables and methods in React.js

I've noticed that I could use private variables like this: 我注意到我可以使用这样的私有变量:

var Hello = React.createClass(new (function(){

    var name;

    this.getInitialState = function() {
        name = "Sir " + this.props.name;
        return null;
    };

    this.render = function() {
        return <div>Hello {name}</div>;
    };
})());

React.render(<Hello name="World" />, document.getElementById('container'));

Why I should not be doing this? 为什么我不应该这样做?

Thank you for any help 感谢您的任何帮助

If you are using es7, you can define class properties as a private variables like this: 如果您使用的是es7,则可以将类属性定义为这样的私有变量:

class Hello extends React.Component {
  name = 'Jack';
  render() {
    return (
      <div>
        {this.name}
      </div>
    );
  }
}
export default Hello;

Be sure to use babel to compile your code with stage 0 务必使用babel来编译阶段0的代码

Private vars are perfect when you need local (private) state information for a component that does NOT change or relate to rendering directly. 当您需要本地(私有)状态信息的组件不会直接更改或与渲染相关时,私有变量是完美的。 Keep in mind, most things do change rendering so I have found that I use private vars rarely. 请记住,大多数事情都会改变渲染,所以我发现我很少使用私有变量。

Also, keep in mind when you add a variable to the class the way you did, its a singleton so it will be shared with all instances of that component. 另外,请记住,当您以类似方式向类添加变量时,它是一个单例,因此它将与该组件的所有实例共享。 This can lead to issues if truly want something private for each instance -- if thats what you want, then you need to declare it in one of the lifecycle methods for the component perhaps like this 如果真的想要为每个实例提供私有内容,这可能会导致问题 - 如果这就是你想要的东西,那么你需要在组件的一个生命周期方法中声明它,也许就像这样

componentDidMount() {
    this.name = 'hello';
},

I don't think there's anything wrong with it. 我不认为它有任何问题。 The syntax is a bit funky, but it's a smart trick. 语法有点时髦,但这是一个聪明的伎俩。

I would question the need for a truly private variable. 我会质疑真正私有变量的必要性。 I can only think of two reasons why someone might want one, but both can be debunked. 我只能想到有人可能想要一个的两个原因,但两者都可以被揭穿。

1) You make a library to be consumed by others... If someone is poking around inside your library code where they're not supposed to be, their either breaking their own experience or working around bugs they have found in your code. 1)你让一个图书馆被其他人消费......如果有人在你的图书馆代码中探究他们不应该的东西,他们要么打破他们自己的经验,要么解决他们在你的代码中发现的错误。 Either way, no harm to you or others. 无论哪种方式,对您或他人都没有伤害。 Worse case, they break their own app. 更糟糕的是,他们打破了自己的应用程序。 Private variables left a really bad taste in my mouth coming from Flex. 私人变量在我的口中留下了非常糟糕的味道来自Flex。 JavaScript's openness is a breath of fresh air IMO. JavaScript的开放性是IMO的新鲜空气。

2) You want to hide private data inside your app... With modern browsers, anything in JavaScript can be inspected and modified at run time. 2)您希望在应用程序中隐藏私人数据...使用现代浏览器,可以在运行时检查和修改JavaScript中的任何内容。 It's impossible to hide data from users in JavaScript. 在JavaScript中隐藏用户数据是不可能的。 You can only make things hard to find. 你只能难以找到。

I know this alternative isn't truly private, but the usage is the same. 我知道这个替代方案不是真正的私有,但用法是一样的。 Since I'm not a big fan of fighting hard to make things private, I'll include it anyway. 因为我不是一个努力使事情变得私密的忠实粉丝,所以无论如何我都会把它包括在内。 ;g) ;G)

var Hello = React.createClass({

    name: null,

    getInitialState: function() {
        this.name = "Sir " + this.props.name;
        return null;
    },

    render: function() {
        return <div>Hello {this.name}</div>;
    };
});

React.render(<Hello name="World" />, document.getElementById('container'));

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

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