Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 为什么我的react函数返回[object,object]_Reactjs - Fatal编程技术网

Reactjs 为什么我的react函数返回[object,object]

Reactjs 为什么我的react函数返回[object,object],reactjs,Reactjs,我的数字组件中有一个函数,它应该返回状态,但是它返回[object,object],我看不出我做错了什么? 我已经用数字编写了一个函数,它在我的应用程序组件中返回另一个函数 import React, { Component } from 'react'; import Numbers from './Numbers' import './App.css'; class App extends Component { constructor(props){ super()

我的数字组件中有一个函数,它应该返回状态,但是它返回[object,object],我看不出我做错了什么? 我已经用数字编写了一个函数,它在我的应用程序组件中返回另一个函数

import React, { Component } from 'react';
import Numbers from './Numbers'
import './App.css';

class App extends Component {
  constructor(props){
    super()
    this.state={
      calcValue:0
    }
  }

  takeValue = (n) => {
    alert(n)
  }

  render() {
    return (
      <div className="App">
        <Numbers submit={(n) => this.takeValue(n)} numberValue={1}/>
      </div>
    );
  }
}

export default App;
import React,{Component}来自'React';
从“./Numbers”导入数字
导入“/App.css”;
类应用程序扩展组件{
建造师(道具){
超级()
这个州={
计算值:0
}
}
takeValue=(n)=>{
警报(n)
}
render(){
返回(
this.takeValue(n)}numberValue={1}/>
);
}
}
导出默认应用程序;
数字组件:

import React, { Component } from 'react';
import propTypes from 'prop-types';

class Numbers extends Component {
  constructor(props){
    super();
    this.state={
      numberValue:6
    }
  }

  submit = (n) => {
    this.props.takeValue(this.state.numberValue)
  }

  render() {
    return (
      <div>
        <button value={this.state.numberValue} onClick={this.props.submit}>
          {this.props.numberValue}
        </button>
      </div>
    )
  }
}

// Completed.propTypes={
// test:propTypes.string.isRequired

export default Numbers
import React,{Component}来自'React';
从“道具类型”导入道具类型;
类号扩展组件{
建造师(道具){
超级();
这个州={
数值:6
}
}
提交=(n)=>{
this.props.takeValue(this.state.numberValue)
}
render(){
返回(
{this.props.numberValue}
)
}
}
//已完成的.propTypes={
//测试:需要propTypes.string.isRequired
导出默认数字

您不理解传递道具的概念。您需要区分要传递给子组件的内容以及希望如何从子组件调用父组件

改变

onClick={this.props.submit}

onClick={this.props.submit}
将调用父组件而不是子组件


您不理解传递道具的概念。您需要区分要传递给子组件的内容以及希望如何从子组件调用父组件

改变

onClick={this.props.submit}

onClick={this.props.submit}
将调用父组件而不是子组件


谢谢,我发现了我的错误!应该是
this.submit
而不是
this.props.submit
我还需要调用
takeValue
函数


谢谢

谢谢我发现了我的错误!应该是
这个。提交
而不是
这个。道具。提交
我还需要调用
takeValue
函数


谢谢

谢谢是的,我看到了我的错误。我是新来的反应者。@Lee是的,它发生了:)谢谢是的,我看到了我的错误。我是新来的反应者。@Lee是的,它发生了:)是的。我想这就是你要找的:这部分是为答案保留的,你可以通过接受他的回答来感谢@Just code是的。我想这就是你要找的:这是您可以通过接受@Just code的答案来感谢@Just code
 submit=(n) => {    
       this.props.takeValue(this.state.numberValue)    
    }
  submit = (n) => {
    this.props.submit(this.state.numberValue)
  }