マッピングとフィルタリングによって順序付けられていないリストに表示されているすべての名前付き色のdefautProps配列があります。私は既に各色の名前を表示していますが、その名前付きの色をインラインスタイルタグのbackgroundColorとして使用したいと思います。失敗したインラインスタイルコードが以下に含まれています。ご意見ありがとうございます。反応コンポーネント内のマップされたリストスタイルプロパティを動的に変更します
ここclass JSexp extends React.Component {
constructor (props) {
super(props);
this.state = {
filterBy: ''
};
}
filterColors =() => {
const clr = document.getElementById('filter').value;
this.setState({ filterBy: clr})
}
getColor = (idx) => {
return this.props.allColors[idx]
}
render() {
const arr = this.props.allColors;
const filterBy = clr => clr.includes(this.state.filterBy);
const backgroundColor = this.getColor(16)
const style = {
width: '20px',
height: '20px',
backgroundColor: this.getColor({idx})
}
const colors = arr.filter(filterBy).map((color, idx) =>
(
<li key={idx}>{color} <div style={style} id={idx} /> </li>
));
return (
<div>
<h3>Named Colors</h3>
<ul>
{colors}
</ul>
<input type="text" placeholder="Filter by" id="filter" onChange={this.filterColors}></input>
</div>
);
}
}
JSexp.defaultProps = {
allColors: ["AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond",
"Blue", "BlueViolet", "Brown", "BurlyWood", "CadetBlue", "Chartreuse", "Chocolate",
"Coral", "CornflowerBlue", "Cornsilk", "Crimson", "Cyan", "DarkBlue", "DarkCyan", "DarkGoldenRod",
"DarkGray", "DarkGrey", "DarkGreen", "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "Darkorange",
"DarkOrchid", "DarkRed", "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkSlateGrey",
"DarkTurquoise", "DarkViolet", "DeepPink", "DeepSkyBlue", "DimGray", "DimGrey", "DodgerBlue",
"FireBrick", "FloralWhite", "ForestGreen", "Fuchsia", "Gainsboro", "GhostWhite", "Gold", "GoldenRod",
"Gray", "Grey", "Green", "GreenYellow", "HoneyDew", "HotPink", "IndianRed", "Indigo", "Ivory", "Khaki",
"Lavender", "LavenderBlush", "LawnGreen", "LemonChiffon", "LightBlue", "LightCoral", "LightCyan",
"LightGoldenRodYellow", "LightGray", "LightGrey", "LightGreen", "LightPink", "LightSalmon",
"LightSeaGreen", "LightSkyBlue", "LightSlateGray", "LightSlateGrey", "LightSteelBlue", "LightYellow",
"Lime", "LimeGreen", "Linen", "Magenta", "Maroon", "MediumAquaMarine", "MediumBlue", "MediumOrchid",
"MediumPurple", "MediumSeaGreen", "MediumSlateBlue", "MediumSpringGreen", "MediumTurquoise",
"MediumVioletRed", "MidnightBlue", "MintCream", "MistyRose", "Moccasin", "NavajoWhite", "Navy",
"OldLace", "Olive", "OliveDrab", "Orange", "OrangeRed", "Orchid", "PaleGoldenRod", "PaleGreen",
"PaleTurquoise", "PaleVioletRed", "PapayaWhip", "PeachPuff", "Peru", "Pink", "Plum", "PowderBlue",
"Purple", "Red", "RosyBrown", "RoyalBlue", "SaddleBrown", "Salmon", "SandyBrown", "SeaGreen",
"SeaShell", "Sienna", "Silver", "SkyBlue", "SlateBlue", "SlateGray", "SlateGrey", "Snow", "SpringGreen",
"SteelBlue", "Tan", "Teal", "Thistle", "Tomato", "Turquoise", "Violet", "Wheat", "White", "WhiteSmoke",
"Yellow", "YellowGreen"]
};
どのような反応のバージョンを使用していますか?私はあなたのコードでいくつかの問題をここで実行しています。上記のスニペットはローカルで実行できますか?あなたはcodesandbox.ioで再作成できますか? – archae0pteryx
私は何が間違っていたのか分かりました。ここで作業コードを見ることができます:https://3l7q41026.codesandbox.io/ –
恐ろしい仲間。私は私が参照してください参照してください... – archae0pteryx