0

I am trying to figure out how to respond to the warning in react to use javascript classes to create components in my MERN app.

The warning says:

Warning: Accessing createClass via the main React package is deprecated, and will be removed in React v16.0. Use a plain JavaScript class instead. If you're not yet ready to migrate, create-react-class v15.* is available on npm as a temporary, drop-in replacement. For more info see[ \[this link\][1]

The link in that message says:

// After (15.5)
var React = require('react');
var createReactClass = require('create-react-class');

var Component = createReactClass({
  mixins: [MixinA],
  render() {
    return <Child />;
  }
});

I am using react v 15.5.4

In my app, I have tried to change my components as follows:

import React from 'react';
import ReactDOM from 'react-dom';
import { Button } from 'react-bootstrap';

var createReactClass = require('create-react-class');


var GreeterForm = createReactClass({
  onFormSubmit: function(e) {
    e.preventDefault();

However, the warning persists. Can anyone see what I have done wrong? How do I implement the new approach to defining components?

2
  • react-bootstrap might still be using React.createClass or you have another component somewhere that uses it? But why not use class as suggested in the warning? Commented Jun 27, 2017 at 0:41
  • That would explain why my bootstrap theme no longer works. Thanks - I'll look into that line of thought. Commented Jun 27, 2017 at 0:43

2 Answers 2

3

You should use ES6 class for make a React component.

import React from 'react';

class App extends from React.Component{
    constructor(props){
        super(props);
        this.sample = this.sample.bind(this);
        // initialize your methods, states here
    }

    // if you want life cycle methods and methods define here

    componentWillMount(nextProps, nextState){
        console.log('componentWillMount');
    }

    sample(){
        console.log('sample');
    }

    render(){
        return <div onClick={this.sample}>Hello World!</div>
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

where would you write a function in this setup?
Using in my React App.
No - I mean where in that component would you write your function ?
In this component has a method(function) named sample. You asked this or not? If your answer is NO Can you asked your doubts in detailed.
1

This is what I would do to create a class in React:

import React, { Component } from 'react';

class GreeterForm extends Component {
    onFormSubmit = (e) => {
        e.preventDefault();
        //do stuff
    }
    render() {
        return (<Child onFormSubmit={this.onFormSubmit} />)
    }
}

2 Comments

Thanks for your example. How does that reflect the advice in this info sheet: facebook.github.io/react/blog/2017/04/07/… ?
The advice given there is for maintaining code that was written before ES2015 (several years ago). Now, instead of using 'react-create-class', you just use standard JavaScript classes. Hence, class ... extends Component

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.