helpers/stringHelper.js
export const getStyleObjectFromString = str => {
if (!str) { return {}; }
const style = {};
str.split(";").forEach(el => {
const [property, value] = el.split(":");
if (!property) return;
const formattedProperty = formatStringToCamelCase(property.trim());
style[formattedProperty] = value.trim();
});
return style;
};
コンポーネントで使用する
import { getStyleObjectFromString } from "helpers/stringHelper";
return (
<div
style={getStyleObjectFromString("color:red; margin:20px; font-weight:bold;")}
/>
)