如何使用数组方法改变div中的不同颜色。在单按钮reactjs中单击
import { use state } from 'react';
import React from 'react';
import './style.css';
export default function Div() {
const [data, set data] = useState('green');
return (
<div class="sec" style={{ 'background-color': data }}>
{data}
<button on Click={() => setData('red', 'orange')}>state click
);
}
import { use state } from 'react';
import React from 'react';
import './style.css';
export default function Div() {
const [data, set data] = useState('green');
return (
<div class="sec" style={{ 'background-color': data }}>
{data}
<button on Click={() => setData('red', 'orange')}>state click
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先=>您应该删除单词“set”和“data”之间的空格,如下所示:
Second =>;在 style 属性中,第二个参数是一个字符串,但在这种情况下,您必须使用反引号(`)来使用“模板文字”,如下所示:
First => You should remove the space between the words "set" and "data" like this:
Second => in the style attribute just the second argument is a string, but in this case you will have to use "Template literals" using backticks(`) like this:
我已经修复了您代码中的拼写错误和错误,并且它似乎工作正常。
单击按钮时,它将背景颜色更改为橙色,但我仍然不明白,您想根据颜色数组更改背景颜色吗?如果是这样,您可以使用 Math.random 来完成,每次单击都会从颜色数组中生成一个随机项目。
I've fixed the typos and errors in your code, and it seems to be working fine.
it changes the background color to orange when the button is clicked, but I still don't understand, do you want to change the color of the background based on an array of colors? if so you can do it using the Math.random, on each click a random item from the colors array will be generated.
这是简短的解释
首先:状态用于存储发生变化的事物。
颜色数组不会改变,所以不要将其存储在状态中。
所选颜色将会改变,因此请将其存储在状态中。在这种情况下,您可以通过使用数组中颜色的索引来做到这一点。
接下来,当颜色发生变化时,您要选择下一种。这只是增加索引的问题。然而,当你到达终点时,你可能会想要循环回来。所以检查一下。
第三,由于您只需要一个按钮,因此只创建一个按钮。不要循环那里的颜色数组。
使用状态值来指定背景颜色。
onclick
是 React 中的onClick
。只需传递设置 nextColor 的函数即可。它不需要任何参数。
最后,如果您想要一个 HTML 按钮元素,那么它就是 。 JSX 名称以大写字母开头意味着您正在使用一个组件。有很多第三方 Button 组件,但您没有导入任何组件。尝试在此处使用将递归地使用您刚刚创建的组件。
Here is short explanation
First: The state is used to store things which change.
The array of colours isn't going to change, so don't store it in the state.
The selected colour is going to change, so do store that in the state. In this case, you can do that by using the index of the colour in the array.
Next, when the colour changes, you want to pick the next one. That's just a matter of incrementing the index. However, when you get to the end you'll probably want to loop back. So check for that.
Third, since you only want one button, create only one button. Don't loop over the array of colours there.
Use the value from the state to assign the background colour.
onclick
isonClick
in React.Just pass the function for setting the nextColour. It doesn't need any arguments.
Finally, if you want an HTML button element then it is . Starting a JSX name with a capital letter means you are using a component. There are plenty of third-party Button components, but you aren't importing any. Trying to use here would be recursively using the component you just created.