![](/img/trans.png)
[英]Functions not valid as a React child. This may happen if you return a Component instead of <Component /> from render
[英]React is showing “Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render”
下面是主要的game.js和app.js文件:
//Heres the game file// import React, { Component } from "react"; class Game extends Component { constructor(props) { super(props); this.state = { num: 0 }; this.numPicker = this.numPicker.bind(this); } numPicker(e) { let rand = Math.floor(Math.random() * this.props.maxNum); return rand; } render() { return ( <div> <h1>Hellow World {this.numPicker}</h1> </div> ); } } export default Game; //Heres the main file:// import React from "react"; import "./App.css"; import Game from "./game"; function App() { return ( <div className="App"> <Game maxNum={1} /> </div> ); } export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
在game.js文件中,我在 H1 中使用了numPicker function。 現在我收到此錯誤:
警告:函數作為 React 子級無效。 如果您返回一個組件而不是從渲染中返回,則可能會發生這種情況。 或者,也許您打算調用此 function 而不是返回它。
您需要調用 numPicker function 返回一個數據,然后可以呈現
<h1>Hellow World {this.numPicker()}</h1>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.