typescript - 如何使用 useState 更新组件?
问题描述
我有一个分段控件库和一个图形库,我想从中使用tickFormat
组件。我想使用这两个库来创建一个换轴系统。例如,如果我在分段控件上单击“月”,则图表应更新其轴以显示所有月份。有没有办法做到这一点?我在想有一种方法可以useState
用来更新tickFormat
组件。
解决方案
您可以使用“useState”在每次渲染之间创建和保存格式,并调用“useState”的函数来更改状态的值。
如果您将状态赋予 tickFormat,则每次更改状态时组件都会重新呈现。
import React, {useState} from 'react';
function myComponent(props) {
const [value, setValue] = useState('your default value or function');
// value is the value of your state created by useState
// setValue is the function to call to change your state created by useState.
// Example : setValue('the new value of your state')
// the name of 'value' or 'setValue' can be change to anything. They are juste given as example
return (
<VictoryAxis
tickFormat={value}
/>
)
}
推荐阅读
- python - 如何检查列表列表中是否存在重复列表?
- mongodb - 如何在不丢失原始结构的情况下重建这个深度展开的对象?
- jquery - 检测文本输入并清除其他文本框
- google-apps-script - 有什么方法可以选择下拉选项并在网站中提交表单并使用 google api 在 google 表格中获取接收到的文件?
- python - 如何使熊猫数据框保持不变并在函数之外使用它?
- javascript - 如何修复警告:validateDOMNesting(...):不能作为孩子出现
- flutter - flutter_local_notifications:“PlatformException(PlatformException(INVALID_ICON,找不到资源))
- pytorch - 修复torch random_split() 的种子
- python - ValueError:无法反序列化关键数据
- c++ - 误导性语法/解析器错误:reinterpret_cast 中预期 > 而不是 *