score:3
Accepted answer
By updating below two methods you will get the solutions.
Pass id
in hide
method.
table = () => {
const tableDetail = Object.keys(postD).map((i) => (
<div key={i}>
<h2>{postD[i].title}</h2>
<p>{postD[i].content}</p>
<span ref={`ref-${postD[i].id}`} id={`test-${postD[i].id}`}>The Quick Brown Fox.</span>
<button onClick={() => this.hide(postD[i].id)} >Like</button>
</div>
));
}
Use same id
to get the ref.
hide = (id) => {
const span = ReactDOM.findDOMNode(this.refs[`ref-${id}`]);
span.className = "hidden";
}
Hope this will help!
score:0
You can use below snippet. I have updated your class. Just pass "id" to the hide method.
class Test extends React.Component {
constructor() {
super();
}
hide = (id) => {
const span = this.refs[`ref-${id}`];
span.ClassName = "hidden";
}
table = () => {
const tableDetail = Object.keys(postD).map((i) => (
<div key={postD[i].id}>
<h2>{postD[i].title }</h2>
<p>{postD[i].content }</p>
<span ref={`ref-${postD[i].id}`} id={`${postD[i].id}`}>The Quick Brown Fox.</span>
<button onClick={() => this.hide(postD[i].id)} >Like</button>
</div>
}
render() {
return (
<>
<h2>Table</h2>
{this.table()}
</>
);
}
}
Source: stackoverflow.com
Related Query
- Is it possible to have a dynamic ref in react js?
- Is it possible to have two states in one react component
- React ref using reactstrap Input Module doesn't have Value prop
- Is it possible to use React Hooks outside of functional component, or i have to use mobx or redux?
- Getting the ref from a dynamic component when using Redux, React and react-router-dom 4.x
- how to test a react hook that can have a ref of an html element passed to it
- Is it possible to add ref of props.children have no ref
- React : How do you write id to have dynamic axios requests
- How to have React evaluate a dynamic variable?
- Is it possible to have dynamic event handlers in React?
- React dynamic form input ref typeError
- Is it possible to have multiple BrowserRouter in react app
- How to get dynamic ref on React
- use ref for parent elements in dynamic blocks React
- Is it possible to have a 'static' react component that can be used globally every page
- How to have dynamic rowspan with antd react
- Is it possible to embed a dynamic react component into a separate html file without downloading a file or using a server?
- React ref for a react element passed as a prop, dynamic event
- Unable to set my React component/jsx to have a dynamic table width
- In React is it possible to pass a prop through a component and have it act as the prop name for a child component
- React - How can I reverse the order of dynamic children lists that have a key unique id
- Have trouble implementing dynamic checklist on react
- React / JSX Dynamic Component Name
- Dynamic tag name in React JSX
- Is it possible to return empty in react render function?
- Is it possible to use if...else... statement in React render function?
- Can I make dynamic styles in React Native?
- How to create dynamic href in react render function?
- Is it possible to use dotenv in a react project?
- Is it possible to use React without rendering HTML?
More Query from same tag
- Why is there a vertical gap between adjacent divs when selecting text?
- React router changes url but not page loading
- React - setTimeout inside promise flashes UI elements, randomly returns array undefined
- Express does not receive parameters via POST from React using Fetch API
- how to attach picture to another picture?
- JS error when executing electron app with native module on other computer
- Why is the setstate not updating the value in ReactJs?
- react-icons/fa fails to install: Attempted import error: 'FaStar' is not exported from 'react-icons/fa'
- G2Plot AntV library. Customize a Bullet Chart tooltip (display names instead of indices)
- React onClick not firing inside map generated list
- Getting error in React.js project
- React Router - How to redirect user if logged in?
- Return promise from React Redux Thunk
- how to prevent external url append to localhost url when using react link?
- React JS trying to map collection, value is not defined
- How do I pass properties to stateless/functional components?
- TS2532 - Object is possibly undefined - useRef and react-three-fiber
- Cross Origin 403 - No Acces-Controll-Allow-Origin - Failed to fetch
- How to use and pass routeprops in react and typescript?
- ReactRouter passing name of component
- React-Testing-Library(RTL): Animation causing TypeError with React Pose
- How can I logout user on route change once JWT token is expired?
- Compiler warning "Module not found: Error: Can't resolve './locale'" since I use momentjs in my react app
- Ant Design React. Bootstrap Grid "container" concept
- Making several API requests at once?
- TypeError: Cannot read property 'map' of undefined.....How to fix it
- Cannot read property '__reactstandin__key' of undefined gatsby graphql
- Get type error cannot destructure property as it is undefined in React
- Cannot set the value of paragraph as a new variable
- Whats the proper way to have a form submit data in a stateless react component?