[英]How to change Button Color and Name in specific cell in Material UI Table
I am using Material Table
for Showing my Data.我正在使用
Material Table
来显示我的数据。 Here it looks like, https://i.postimg.cc/4x6hbm5N/image.png这里看起来像, https://i.postimg.cc/4x6hbm5N/image.png
I have declared a state
above in my code我已经在我的代码中声明了一个
state
const [sharedOrdersList, setShareOrdersList] = useState([])
When the User
click the Share
button, I will push the Order Id
to sharedOrdersList
.当
User
单击Share
按钮时,我会将Order Id
推送到sharedOrdersList
。 It works.有用。 But now my issue was i want to change the Button Name to
UnShare
and button color to another color.但现在我的问题是我想将按钮名称更改为
UnShare
并将按钮颜色更改为另一种颜色。 I tried several ways.我尝试了几种方法。 But I don't know how to make it
但我不知道如何制作
Here's my Function Code:这是我的功能代码:
const shareOrderBtnClick = (orderID) => {
sharedOrdersList.push(orderID)
alert("share Btn Clicked")
console.log(sharedOrdersList)
}
Return Code:返回代码:
allOrderDatas.map((row) => (
<TableRow>
<TableCell">
<Button variant="contained"
onClick={()=>{shareOrderBtnClick(row.orderID)}}
style={{ backgroundColor:sharedOrdersList.includes(row.orderID)?"#59886B":"#D54062",}}>
<ShareIcon />{sharedOrdersList.includes(row.orderID)?"UnShare":"Share"</Button>
</TableCell>
</TableRow>
I don't know how to change the specific item in the cell in the row in table.我不知道如何更改表格行中单元格中的特定项目。 Please Help me with some solutions.
请帮我解决一些问题。
states in react are immutable.反应中的状态是不可变的。
useState
returns a value and an updater function. useState
返回一个值和一个更新程序函数。 You can't directly change the value.您不能直接更改该值。 To update the state you need to call
setShareOrdersList
function with the new value.要更新状态,您需要使用新值调用
setShareOrdersList
函数。
In you're example在你的例子
const shareOrderBtnClick = (orderID) => {
setShareOrdersList([... sharedOrdersList, orderID]);
// OR BETTER
// setShareOrdersList(prevState => [...prevState, orderID]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.