0
Good afternoon!
I would like to know how to solve this problem.
I have a child component that’s like a menu. It contains buttons and currently - according to the click on the functions that are in the parent component - will display other components. I just don’t want the functions below to be in the parent component. I would like such functions to be in the child.
Function:
const onChangeActive = (event, value) => {
setSelectionActive(value)
}
const onChangeType = (event, value) => {
setChartType(value)
}
But if I put in the child I lose access to the Tates and their changes I need in the father. The Tates are: chartType
and the selectionActive
. So, I need his states - which in this case are chartType
and the selectionActive
- accessible in the Parent component.
Component Filho:
const Filho ={type, buttonActive, onChangeType = () => { }, onChangeActive = () => { } ) =>{
return(
<ToggleButton value={type} onChange={onChangeType}>
<Button disabled={buttonActive===TABLE}>button 1</Button>
<Button disabled={buttonActive===TABLE}>button 2</Button>
</ToggleButton>
<ToggleButton value={buttonActive} onChange={onChangeActive}>
<IconButton>
<Span title="chart" />
</IconButton>
<IconButton>
<Span title="table" />
</IconButton>
</ToggleButton>
)}
Component Pai:
const Pai = () => {
const [chartType, setChartType] = useState(1)
const [selectionActive, setSelectionActive] = useState(1)
const onChangeActive = (event, value) => {
setSelectionActive(value)
}
const onChangeType = (event, value) => {
setChartType(value)
}
return(
<Filho buttonActive={selectionActive}
onChangeType={onChangeType}
onChangeActive={onChangeActive} />
{selectionActive === SELECT_CHART ? renderOpcao1() : renderOpcao2()}
)
}
How I can take these functions onChangeActive
and onChangeType
of the father, leave in the son but in the father continue with access to the States chartType
and selectionActive
in the parent component?
You’ve already read the section Elevating the State of the React documentation? There is a relatively detailed explanation that can help you. :)
– Luiz Felipe
worked, thanks !!
– mari