Convert React class to ES6 class error when use context
See original GitHub issueI 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:
- Created 8 years ago
- Comments:8 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The correct way:
Note that I removed comma after
getChildContext
method and movedchildContextTypes
to declare attribute on the component class.@oliviertassinari it needs to be a prototype method (non-static). It can also use state and props.