简体   繁体   English

将“类”“实例”传递给 ReactJS 组件以代替 `props` 文字

[英]passing "class" "instances" to ReactJS components in lieu of `props` literals

It seems that when it comes to the props object provided in React.createElement , the createElement method only checks the enumerable properties of that object (or so it appears at least).似乎当涉及到React.createElement提供的props对象时, createElement方法只检查该对象的可枚举属性(或者至少看起来如此)。

So, if I have my ReactJS state modeled using an ECMAScript-6 class with methods to alter that state (encapsulation, right?), I can't just pass that state to a ReactJS component in the call to createElement as ReactJS won't see the methods.因此,如果我使用 ECMAScript-6 类对我的 ReactJS 状态进行建模,该类具有更改该状态的方法(封装,对吗?),我不能在调用createElement将该状态传递给 ReactJS 组件,因为 ReactJS 不会见方法。 So I have to do things like this:所以我必须做这样的事情:

const MyApp = React.createClass({
      propTypes: {
          methodA: React.PropTypes.func.isRequired,
          methodB: React.PropTypes.func.isRequired
      },
...});

class MyAppState {
...
}

let appState = new MyAppState();
ReactDOM.render(
    React.createElement(MyApp, Object.assign(
         {
          methodA: s.methodA.bind(s),
          methodB: s.methodB.bind(s)
         }, appState))
    , document.getElementById('somePlace')
)

... which sort of beats the purpose. ......哪种击败了目的。 I am no big fan of Javascript "classes", but is there a way for classes to be used in that way to provide props for ReactJS elements?我不是 Javascript“类”的忠实粉丝,但是有没有办法以这种方式使用类来为 ReactJS 元素提供props Class "fields" are enumerable and so pose no problem, it's the methods/actions that I can't pass this way.类“字段”是可枚举的,因此没有问题,这是我无法通过这种方式传递的方法/操作。

The problem you're having is that your methods aren't defined on the class instance, they're defined on the constructor's prototype .您遇到的问题是您的方法不是在类实例上定义的,而是在构造函数的原型上定义的

When React iterates over the class instance it won't iterate over the members on the prototype.当 React 迭代类实例时,它不会迭代原型上的成员。

It's simpler to model your application state as a regular object and you'll probably find that approach makes your application more predictable.将您的应用程序状态建模为常规对象更简单,您可能会发现这种方法使您的应用程序更具可预测性。

Using an object will also allow you to serialize your state for saving and reading as JSON (there's no way to serialize a class).使用对象还允许您序列化您的状态以保存和读取为 JSON(无法序列化类)。

If you are using class because you need to make multiple instances of your state, then you can use a factory function instead.如果您使用类是因为您需要创建多个状态实例,那么您可以改用工厂函数。

function makeAppState(foo, bar, baz) {
  return {
    qux: foo + bar - baz
  };
}

If you're using a class because you want to directly manipulate your state with some methods, then you can also achieve this with a factory function and a prototype.如果您使用类是因为您想通过某些方法直接操作您的状态,那么您也可以使用工厂函数和原型来实现这一点。

function makeAppState(foo, bar, baz) {
  var state = Object.create(AppStatePrototype);
  state.qux = foo + bar - baz;
  return state;
}

var AppStatePrototype = {
  update() {
    this.qux = 0;
  }
};

At least in this case, it's much clearer to me that the update method won't be present on the object returned from makeAppState .至少在这种情况下,我更清楚的是update方法不会出现在从makeAppState返回的对象上。

If you want to pass the methods and properties together, just return an object literal with both.如果您想将方法和属性一起传递,只需返回带有两者的对象字面量即可。

function makeAppState(foo, bar, baz) {
  return {
    qux: foo + bar - baz,
    update() {
      this.qux = 0;
    }
  };
}

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

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