Some experimentation I'm doing with React has gotten me the following code:
const BreadNav = props => {
const initial={
stack:[
{
name:"Home",
render:React.cloneElement(
props.children,
{pushElement:pushElement}
),
state:{}
}
],
};
const [state, setState] = React.useState(initial);
const pushElement = (oldState,elem) => {
let newStack = JSON.parse(JSON.stringify(state.stack));
newStack[newStack.length-1].state = oldState;
newStack.push(elem);
setState({
...state,
stack:newStack
});
}
return(
state.stack[state.stack.length-1].render
);
}
React gives me ReferenceError: can't access lexical declaration 'pushElement' before initialization
which makes sense for the order the lines are in. If this was C, I would just throw in a function prototype to declare pushElement
and define it later, but I haven't seen a javascript answer for this. How do I avoid this trouble?