如何使用 useState 更新组件?

问题描述

我有一个 segmented control library一个 graph library,我想从中使用 tickFormat 组件。我想使用这两个库来创建一个轴更改系统。例如,如果我在分段控件上单击 Month,则图表应更新其轴,显示所有月份。有没有办法做到这一点?我在想有一种方法可以使用 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}
    />
  )

}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...