sir-haver
sir-haver

Reputation: 3592

Extending react component and an additional class

I was trying to go the simplest way using Object.assign:

import React, { Component } from 'react'
import { StyleSheet, View, Text, Button } from 'react-native'

class MySuperClass {
  firstMethod() {
     console.log("method 1")
  }
}

const a = Object.assign({}, Component, MySuperClass)

  class CategoriesScreen extends a {

    render() {
      return (
        <View>

        </View>
    );
    }
  };

Yet I'm receiving the error :

TypeError: SUper expression must either be null or a function

I researched it but it fits to different cases than this one

How can I extend one object that includes both the MySuperClass and the react Component?

(PLEASE NOTE: I know I can extend Component in MySuperClass as a work around, but I'm interested to know how to achieve it with this approach, thanks)

Upvotes: 0

Views: 26

Answers (1)

Tom Slutsky
Tom Slutsky

Reputation: 724

You can do

Object.assign(MySuperClass.prototype, Component.prototype);

here is a more complete example: https://codesandbox.io/s/react-component-with-objectassign-c9es0

Upvotes: 1

Related Questions