【发布时间】:2018-08-20 11:49:34
【问题描述】:
我有两个 js 文件 Child.js 和 App.js。
Child.js
import React from 'react';
const Child = (props) =>{
<div>
<button onClick={props.doWhatever}>{props.title}</button>
</div>
}
export default Child;
App.js
import React, { Component } from 'react';
import './App.css';
import Child from './components/parentTochild/Child.js'
class App extends Component {
state = {
title : 'Helloooo'
}
changeWorld = (newTitle) => {
this.setState = ({
title : newTitle
});
}
render() {
return (
<div className="App">
<Child doWhatever={this.changeWorld.bind(this , 'New world')} title={this.state.title}/>
</div>
);
}
}
export default App;
在执行此代码时,我收到了标题中提到的错误。我试图解决它。但我无法弄清楚这段代码有什么问题。
当我删除 <Child doWhatever={this.changeWorld.bind(this , 'New world')} title={this.state.title}/> 并键入它显示在屏幕上的文本时。问题在于使用 Child 组件时。
【问题讨论】:
-
你需要在
Child添加return语句或者使用隐式returnconst Child = props => ( /* notice ( not {*/ <div> -
必须在 Child.js 中包含 return 语句
标签: reactjs parent-child