You can use a function declaration, which is "hoisted", instead of assigning a function value to a const
variable:
function BreadNav(props) {
const initial = {
stack: [
{
name: "Home",
render: React.cloneElement(props.children, {pushElement}),
state: {}
}
],
};
const [state, setState] = React.useState(initial);
function 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;
}
but in this case it would be much more appropriate to define the function before you use it in the initial
object literal:
const BreadNav = props => {
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
});
};
const initial = {
stack: [
{
name: "Home",
render: React.cloneElement(props.children, {pushElement}),
state: {}
}
],
};
const [state, setState] = React.useState(initial);
return state.stack[state.stack.length-1].render;
}
You can still refer to the state
and setState
variables declared below, they are in scope, you just must make sure not to call the function before they are initialised.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…