question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert React class to ES6 class error when use context

See original GitHub issue

I have a Class A created by createClass, while after I transform it into a ES6 class, I caught an err:

//Original class code
var A = React.createClass({

  childContextTypes: {
       name: React.PropTypes.string
  },

  getChildContext: function() {
       return { name: "Jonas" };
  },

  render: function() {
       return <B />;
  }
});
// ES6 class
class A extends React.Component {
  childContextTypes: { 
    name: React.PropTypes.string
  },

  getChildContext () {
    return { name: "Jonas" };
  },

  render () {
    return <B />;
  }
}

May be something wrong in the childContextTypes defining method?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
andreypoppcommented, Sep 14, 2015

The correct way:

class A extends React.Component {
  getChildContext () {
    return { name: "Jonas" };
  }

  render () {
    return <B />;
  }
}

A.childContextTypes = { 
  name: React.PropTypes.string
};

Note that I removed comma after getChildContext method and moved childContextTypes to declare attribute on the component class.

1reaction
brigandcommented, Oct 9, 2016

@oliviertassinari it needs to be a prototype method (non-static). It can also use state and props.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve `this` in converting React module to ES6 class
Just replace all occurrences of 'props' with 'state' in your code, and all will be good. Also you probably want the store's initial...
Read more >
React Without ES6
In ES6 classes, you can define the initial state by assigning this.state in the constructor: class Counter extends React.Component { constructor ...
Read more >
How to convert a React Class Component to a Function ...
Both components take a prop (name) and render `Hello, {name}`. It's an extremely simple example but already we can see some of the...
Read more >
React Binding Patterns: 5 Approaches for Handling `this`
1. Use React.createClass · 2. Bind in Render. The rest of these approaches assume you're declaring React components via ES6 classes. · 3....
Read more >
5 reasons not to use ES6 classes in React - krawaller
...we have to fix the context ourselves, like a well-behaved dog. Either by setting it in the class constructor... constructor() { super(); this.buyMoreBeer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found