I am using a Create-React-App through Codesandbox using hook capable versions. I am trying to create a simple toggle using newer React options of Stateless functional ponents, and hooks. I created a renderprop pattern using children prop, but am getting a "children is not a function error". Professor Google has failed me.
App.js
import React from "react";
import ReactDOM from "react-dom";
import Toggle from "./Toggle";
import "./styles.css";
const App = () => {
return (
<div className="App">
<Toggle>
<Toggle>
{({ show, toggleShow }) => (
<div>
{show && <h1>Show Me</h1>}
<button onClick={toggleShow}>Show / Hide</button>
</div>
)}
</Toggle>
</Toggle>
</div>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
and Toggle.js
import { useState } from "react";
const Toggle = ({ children }) => {
let [show, setShow] = useState(false);
const toggleShow = () => setShow((show = !show));
return children({ show, toggleShow });
};
export default Toggle;
CodeSandbox
I am using a Create-React-App through Codesandbox using hook capable versions. I am trying to create a simple toggle using newer React options of Stateless functional ponents, and hooks. I created a renderprop pattern using children prop, but am getting a "children is not a function error". Professor Google has failed me.
App.js
import React from "react";
import ReactDOM from "react-dom";
import Toggle from "./Toggle";
import "./styles.css";
const App = () => {
return (
<div className="App">
<Toggle>
<Toggle>
{({ show, toggleShow }) => (
<div>
{show && <h1>Show Me</h1>}
<button onClick={toggleShow}>Show / Hide</button>
</div>
)}
</Toggle>
</Toggle>
</div>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
and Toggle.js
import { useState } from "react";
const Toggle = ({ children }) => {
let [show, setShow] = useState(false);
const toggleShow = () => setShow((show = !show));
return children({ show, toggleShow });
};
export default Toggle;
CodeSandbox
Share Improve this question edited Feb 11, 2019 at 7:37 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Jan 25, 2019 at 21:59 cycle4passioncycle4passion 3,3413 gold badges19 silver badges29 bronze badges 1-
But
children[0]
should be one. – Jonas Wilms Commented Jan 25, 2019 at 22:04
1 Answer
Reset to default 6<Toggle>
<Toggle>
The outer Toggle has as its children
another Toggle ponent, so this is where the exception is being thrown. The inner Toggle would be fine since its children
is indeed a function, but the exception prevents the rendering from getting there.
I'm not quite sure what your goal is nesting Toggles inside toggles, so perhaps the fix is to just delete one of them. Alternatively, if your intention is to allow non-functions as children, then you can modify your Toggle ponent to something like this:
const Toggle = ({ children }) => {
let [show, setShow] = useState(false);
const toggleShow = () => setShow((show = !show));
if (typeof children === 'function') {
return children({ show, toggleShow });
}
return children;
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745539387a4632056.html
评论列表(0条)