简体   繁体   English

从react.js中的api获取随机颜色?

[英]Fetch random color from api in react.js?

How can I fetch random color from this API link http://www.colr.org/json/color/random and with that fetched color how do I change each element in the list separately, like add each color for one line of list in react.js?如何从此 API 链接http://www.colr.org/json/color/random获取随机颜色,并使用获取的颜色如何分别更改列表中的每个元素,例如为一行列表添加每种颜色在 react.js 中?

class FetchRandomColor extends React.Component {
state = {
    loading: true,
    color: null,
};

async componentDidMount() {
    const url = "http://www.colr.org/json/color/random";
    const response = await fetch(url);
    const data = await response.json();
    this.setState({color: data.colors[0]})
}


render() {
return (
    <div>
        {this.state.loading || !this.state.color ? (
        <div>loading...</div> 
        ) : (
        <div>
           div>{this.state.color}</div>
        </div>
        )}
    </div>
);

} } } }

render(, document.getElementById('root')); render(, document.getElementById('root'));

I tried this so far I can't find a way to take the color from the api and after that I need to implement that color to each line of the list到目前为止,我尝试过这个我找不到从 api 中获取颜色的方法,之后我需要将该颜色实现到列表的每一行

我认为你可以使用 Math.random() 和 RGBa,这是一个例子https://www.freecodecamp.org/forum/t/react-random-color-change/227595

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM