Inline Style Objects Move Outside

Inline Style Objects Move Outside

Medium ReactJS Performance & Patterns 22 views
Explanation Complexity

Problem Statement

Refactor a component to move style objects outside and keep render clean.

Input Format

No input.

Output Format

Render a React component.

Constraints

Declare styles as constants outside the component.

Input / Output Format

Input Format
No input.
Output Format
Render a React component.
Constraints
Declare styles as constants outside the component.

Examples

Input:
Output:
(React code)

Example Solution (Public)

ReactJS
import React, { useState } from 'react';

const box = { border: '1px solid #eee', borderRadius: 14, padding: 14, width: 420 };
const btn = { padding: '10px 14px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' };

export default function App() {
  const [likes, setLikes] = useState(0);

  return (
    <div style={{ padding: 16 }}>
      <div style={box}>
        <strong>meetcode</strong>
        <div style={{ marginTop: 10, color: '#555' }}>Likes: {likes}</div>
        <button type='button' onClick={() => setLikes((n) => n + 1)} style={{ ...btn, marginTop: 10 }}>Like</button>
      </div>
    </div>
  );
}

Official Solution Code

import React, { useState } from 'react';

const box = { border: '1px solid #eee', borderRadius: 14, padding: 14, width: 420 };
const btn = { padding: '10px 14px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' };

export default function App() {
  const [likes, setLikes] = useState(0);

  return (
    <div style={{ padding: 16 }}>
      <div style={box}>
        <strong>meetcode</strong>
        <div style={{ marginTop: 10, color: '#555' }}>Likes: {likes}</div>
        <button type='button' onClick={() => setLikes((n) => n + 1)} style={{ ...btn, marginTop: 10 }}>Like</button>
      </div>
    </div>
  );
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.