Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the built in Object.assign in react?

Tags:

reactjs

React used to have a merge util, that is now deprecated and replaced with https://github.com/facebook/react/blob/dca7ffbe21d2b27d0c7ff898bbaa27dab84e4043/src/stubs/Object.assign.js

Just wonder if it's possible to use this version of assign in my code? Is it somehow possible to include it? If so, what path should I use?

thanks.

like image 720
noreflow Avatar asked Nov 24 '14 12:11

noreflow


People also ask

What is object assign in React JS?

Object.assign() The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

How do you access an object inside an object in React?

If we want to access all the values of nested objects then we have to use recursion to access each and every level of that object. And it can get more complicated according to the nesting of the object. That why we have to use recursion to get all the values and access the whole nested object.

How do you get data from an object in React?

Use the target. dataset property to access data attributes from the event object in React. The dataset property provides read and write access to the custom data attributes of the element. The property returns a Map of strings which can be converted to an object.

How do you assign an object in JavaScript?

assign() which is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target. It returns the target object which has properties and values copied from the target object.


2 Answers

You can use object.assign package. It is an ES6 Object.assign() "ponyfill".

package.json:

"dependencies": {
    "object-assign": "^1.0.0",
    "react": "^0.12.0",
    ...
  },

Then where you want to use it:

var assign = require('object-assign');

var MessageStore = assign({}, EventEmitter.prototype, {

  emitChange: function() {
  ...

The Facebook Flux Chat example uses it.

like image 171
nilgun Avatar answered Oct 16 '22 19:10

nilgun


I'm using Object.assign shipped with react 0.12 (require('react/lib/Object.assign'))

WARNING: this is private API which could change anytime so you can't really rely on it.

like image 26
Andrei Dziahel Avatar answered Oct 16 '22 19:10

Andrei Dziahel