Inline Style Objects Move Outside
ReactJS
Medium
6 views
Problem Description
Refactor a component to move style objects outside and keep render clean.
Output Format
Render a React component.
Constraints
Declare styles as constants outside the component.
Official Solution
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>
);
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!