I am a React beginner.
I have a Parent component that has multiple input fields (InputField child component). OnFocus I want to toggle a child of Inputfield(InputFieldToolTip component) to show/hide from a method in the parent component
Currently, when I set the state of the showHide and pass into the Input field component via props, it toggles all the tooltip components
How do I reference an individual tooltip component in React? In jQuery, I would just give it an ID and use a Dom selector.
Example code (please don't take this as actual code, it's just a representative of the structure and what's being passed currently):
Parent Component
import React from 'react';
import ReactDOM from 'react-dom';
import InputField from './InputField.jsx';
class Parent extends React.Component {
constructor(props){
super(props);
this.state={
showHide: ' hide',
toolTip: 'This is a tooltip'
}
this.showHide = this.showHide.bind(this);
}
showHide(){
if(this.state.showHide === 'hide') {
this.setState({
showHide: 'show'
))};
} else {
this.setState({
showHide: 'hide'
});
}
}
render(){
return(
<div>
<InputField
name='input-1'
OnFocus={this.showHide}
type="text"
showHide={this.state.showHide}
toolTip={this.state.toolTip}
/>
<InputField
name='input-2'
OnFocus={this.showHide}
type="text"
showHide={this.state.showHide}
toolTip={this.state.toolTip}
/>
<InputField
name='input-3'
OnFocus={this.showHide}
type="text"
showHide={this.state.showHide}
toolTip={this.state.toolTip}
/>
</div>
)
}
}
InputField Component
import React from 'react';
import ReactDOM from 'react-dom';
import InputField from './ToolTip.jsx';
export class InputField extends React.Component{
render(){
return(
<div>
<Input/>
<ToolTip
showHide={this.props.showHide}
toolTip={this.props.toolTip}
/>
</div>
)
}
}
ToolTip Component
import React, {Component} from 'react';
export const ToolTip = (props) => {
return <div className={this.props.showHide}>{this.props.toolTip}</div>
}