【发布时间】:2020-10-18 19:55:49
【问题描述】:
我有如下渲染功能。我正在从我们当前的站点获取一个开源库并将其构建到 react 中,它会创建一个颜色选择器(当我们开始工作时),http://www.booneputney.com/jquery-hex-colorpicker.html Demo 1。我们希望继续使用它,因为它是一种很好的简单颜色选择器。
我试图将选择器的动态构建移动到 render() 函数中的唯一问题。我对 React 相当陌生,我已经到处搜索了答案......但它会在浏览器中呈现标签等。
undefined</div><div className = "picker-sidebar">
</div><div className = "picker-form-wrapper"><form className = "picker-form" onsubmit=
{this.submitColorChoice}><input type="text" name="selected-color" className =
"selected-color" readonly="readonly"/><input type="submit" value="OK" name="submit"
className = "submit"/></form></div></div>
有什么解决办法吗?
感谢您的帮助。
render() {
let output;
{
for (let row = 0; row < this.state.settings.maxBlocks; row++) {
let blocksCount = this.state.settings.size + row;
if (row >= this.state.settings.size) {
blocksCount = this.state.settings.size * 2 - (row - this.state.settings.size + 2);
}
output += "<div className = 'picker-row'>";
for (let block = 0; block < blocksCount; block++) {
let y = centerBlock - row;
let x = -centerBlock + (block + (this.state.settings.maxBlocks - blocksCount) / 2);
let radius = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
let normRadius = radius / maxRadius;
angle = Math.atan(y / x) * 180 / Math.PI + 90;
if (x >= 0) {
angle += 180;//compensate for right 2 quadrants
}
if (settings.colorModel === "hsv") {
valueLightness = 1;
} else {
valueLightness = 0.5;
}
if (normRadius === 0) {
angle = 0;
}//force angle to prevent undefined
output += "<div className = 'color-block' style='background-color:" + this.colorizeBlock(normRadius, angle, valueLightness) + "'></div>";
}
output += "</div>";
}
output += "</div>";
output += '<div className = "picker-sidebar">';
for (let row = 0; row < this.state.settings.maxBlocks; row++) {
output += "<div className = 'color-block'></div>";
}
output += '</div>';
output += '<div className = "picker-form-wrapper"><form className = "picker-form" onsubmit={this.submitColorChoice}>' +
'<input type="text" name="selected-color" className = "selected-color" readonly="readonly"/>' +
'<input type="submit" value="OK" name="submit" className = "submit"/>' +
'</form></div>';
output += "</div>";//end of hex-color-picker-wrapper
}
const colourPicker = <React.Fragment>{output}</React.Fragment>
return (colourPicker)
}
【问题讨论】: