Home > Back-end >  React Class component covert to function component
React Class component covert to function component

Time:01-25

Problem is static variable.

class component use static variable like this.

// component.js
<ProgressWindow messageSet={ProgressWindow.MESSAGE_FOR_REPORT} />
 
// ProgressWindow.js
class ProgressWindow extends React.Component {
  static  MESSAGE_FOR_REPORT = 'report';
  static MESSAGE_FOR_SETTING = 'setting';
}

My mission is that static variable's convert to function component.

I need get messageSet value in new function ProgressWindow component.

How can i?

CodePudding user response:

try this

const ProgressWindow = (props) => {
    return (
        <div>{props.MESSAGE_FOR_REPORT}</div>
    );
};

ProgressWindow.MESSAGE_FOR_REPORT = 'report';
ProgressWindow.MESSAGE_FOR_SETTING = 'setting';

export default ProgressWindow;

There is alternative static variable in Functional Component

ProgressWindow.MESSAGE_FOR_REPORT = 'report';
ProgressWindow.MESSAGE_FOR_SETTING = 'setting';
  • Related