javascript - React Render prop with Children in a functional component with useState Hook - Stack Overflow

I am using a Create-React-App through Codesandbox using hook capable versions. I am trying to create a

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
Add a ment  | 

1 Answer 1

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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信