如何使用 useState 更新组件?

时间:2021-05-03 14:33:34

标签: typescript react-native react-hooks use-state victory-charts

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

1 个答案:

答案 0 :(得分:0)

您可以使用“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}
    />
  )

}