I'm sorry if the title doesn't explain much. I have a react component, which has a button and some texts, and I used it 3 times. I want each button to display its text when I click on it, but hide the other texts.
The code I've written makes me able to display the texts when I click on each button, but it doesn't hide the other buttons' texts. I can make 3 components, one for each button, but I want to try this method, and I'm not sure i can make the other one work too
React component:
import React from 'react'
export default class Button extends React.Component {
state={
display: 'none'
}
render() {
return (<div>
<button
onClick={()=>this.setState({display:'block'})}
style={{ width: "50px", height: "50px", display:"inline-block" }}>
{this.props.children}
</button>
<h1 style={{display: this.state.display}}>
{this.props.title}
</h1>
<img style={{display: this.state.display}} src={this.props.src} alt=""></img>
<p style={{display: this.state.display}}>
{this.props.descri}
</p>
</div>)
}
}
App.js:
import React from 'react';
import './App.css';
import Button from './sarra.js';
import Aa from './Aa.jpg';
import kacem from './b1.jpg';
import harold from './kacem.JPG';
/*
const person = [
{name:"sarah",
imgsrc:"" ,
description: "ipsum lorem "
},
{name:"sarah",
imgsrc:"" ,
description: "ipsum lorem "
},
{name:"sarah",
imgsrc:"" ,
description: "ipsum lorem "
}
]
*/
const isShown =() =>{
Button.style="backgroundColor: red"
}
function App() {
return (<div>
<Button title="Aa" descri="this is ali's profile" src={Aa} >ali</Button>
<Button title="harold" descri="this is harold's profile" src={harold} onClick={isShown()}>harold</Button>
<Button title="kacem" descri="this is kacem's profile" src={kacem} >kacem</Button>
</div>
);
}
export default App;
I also thought about making a function to hide the other buttons, so I made a simple function to start simply, which is the isShown that makes the button red, but it didn't work. i don't know what's wrong, so if i know where the fault is, i can change it so that it changes the other buttons' display to hidden, using their id.
Maybe I can use the state too? in the component, where the onclick props is defined, is it possible to write another line, making the display of the other buttons hidden, with the other one
this.setState({display:'block'}
still there? is there something that's contrary to this to affect the other elements?
If possible, you could also show me how to make the three buttons in the same line. Thank you!