How can you use useState to update a component?

199 Views Asked by At

I have a segmented control library and a graph library that I want to use the tickFormat component from. I'd like to use both these libraries to create an axis-changing system. For example, if I click Month on the segmented control, the graph should update its axis showing all the months. Is there a way to do this? I was thinking there was a way to use useState to update the tickFormat component.

1

There are 1 best solutions below

2
On BEST ANSWER

You can use 'useState' to create and save your format between each render and call the function of your 'useState' to change the value of your state.

if You give the state to tickFormat, the component will rerender each time you change your state.

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}
    />
  )

}