React js onClick can't pass value to method

后端 未结 30 1706
北荒
北荒 2020-11-22 07:05

I want to read the onClick event value properties. But when I click on it, I see something like this on the console:

SyntheticMouseEvent {dispatchConfig: Ob         


        
30条回答
  •  粉色の甜心
    2020-11-22 07:21

    Implementing show total count from an object by passing count as a parameter from main to sub components as described below.

    Here is MainComponent.js

    import React, { Component } from "react";
    
    import SubComp from "./subcomponent";
    
    class App extends Component {
    
      getTotalCount = (count) => {
        this.setState({
          total: this.state.total + count
        })
      };
    
      state = {
        total: 0
      };
    
      render() {
        const someData = [
          { name: "one", count: 200 },
          { name: "two", count: 100 },
          { name: "three", count: 50 }
        ];
        return (
          
    {someData.map((nameAndCount, i) => { return ( ); })}

    Total Count: {this.state.total}

    ); } } export default App;

    And Here is SubComp.js

    import React, { Component } from 'react';
    export default class SubComp extends Component {
    
      calculateTotal = () =>{
        this.props.getTotal(this.props.count);
      }
    
      render() {
        return (
          

    Name: {this.props.name} || Count: {this.props.count}

    ) } };

    Try to implement above and you will get exact scenario that how pass parameters works in reactjs on any DOM method.

提交回复
热议问题