1

I'm trying to pass method ref to functional component but somehow it doesn't work

Here is the function:

import { FaTrashAlt } from 'react-icons/fa';

const ArticlesData = props => {
    return(
        props.products.map(product => {
            return (   
                <tr>
                    <td>{product.name}</td>
                    <td>{product.description}</td>
                    <td>{product.price}$</td>
                    <td>
                        <span className="removeProduct--Container" onClick={props.click}>
                            <FaTrashAlt className="remove--Icon" />
                        </span>
                    </td>
                </tr>
            )
        }).reverse()
    )
}

export default ArticlesData;

Here is the request I'm trying to pass:

onRemove = (id) => {
    fetch(`http://localhost:5000/products/:${id}/delete`, {
        method: 'POST'
    })
}

And here is how I pass:

<ArticlesData products={this.state.products} click={this.onRemove}/>

Update:

controller:

router.post('/:id/delete', (req, res) => {
    try {
        console.log(req.params.id)
        productService.deleteOne(req.params.id)
        res.status(200)
    } catch (error) {
        console.log(error)
    }
})

service:

function deleteOne(id) {
    return Product.deleteOne({_id: id});
}

2 Answers 2

1

You need to call the function with parameter id. I'm assuming your product object has id attribute:

import { FaTrashAlt } from 'react-icons/fa';

const ArticlesData = props => {
    return(
        props.products.map(product => {
            return (   
                <tr>
                    <td>{product.name}</td>
                    <td>{product.description}</td>
                    <td>{product.price}$</td>
                    <td>
                        <span className="removeProduct--Container" onClick={() => props.click(product.id)}>
                            <FaTrashAlt className="remove--Icon" />
                        </span>
                    </td>
                </tr>
            )
        }).reverse()
    )
}

export default ArticlesData;
Sign up to request clarification or add additional context in comments.

1 Comment

I tried it but nothing happend. Is it possible to be something in the passing in click={...} ?
0

Change ArticlesData component's code

from onClick={props.click}

to onClick={() => props.click(product.id)}


Full code:

import React from "react";
import ArticlesData from "./ArticlesData";

export default class SomeName extends React.Component {
  onRemove = (id) => {
    console.log(id);
    fetch(`http://localhost:5000/products/:${id}/delete`, {
        method: 'POST'
    })
  };

  render() {
    return (
      <>
        <ArticlesData click={this.onRemove} />
      </>
    );
  }
}
import { FaTrashAlt } from 'react-icons/fa';

const ArticlesData = props => {
    return(
        props.products.map(product => {
            return (   
                <tr>
                    <td>{product.name}</td>
                    <td>{product.description}</td>
                    <td>{product.price}$</td>
                    <td>
                        <span className="removeProduct--Container" onClick={() => props.click(product.id)}>
                            <FaTrashAlt className="remove--Icon" />
                        </span>
                    </td>
                </tr>
            )
        }).reverse()
    )
}

export default ArticlesData;

CodeSandbox Demo

4 Comments

I tried it but nothing happend. Is it possible to be something in the passing in click={...} ?
Updated my answer. Please check working live codesandbox demo.
I'm using MERN Stack. I see that in router.post delete request when I log the id it is showing and in the browser console it says "Fetch finished loading: POST "localhost:5000/products/:606257f34a43ed20fc6546c4/delete". But it doesn't want to delete :D the product is still there :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.