How can I call a component with a react button? [duplicate] - reactjs

This question already has answers here:
onClick doesn't render new react component.
(5 answers)
Closed 3 years ago.
Sorry if this topic is probably a copy of another one, but I am still with some doubts and I could not solve reading this related topics:
onClick doesn't render new react component.
How can I call my 'Survey' component from the react button, using onClick?
I am using "onClick={Survey}" but it is not working, there is another way to do it or it is something wrong with this kind of call.
class App extends Component {
render() {
return (
<div className="App">
<Container>
<Row>
<Col><ClientInformation /></Col>
</Row>
<Row>
<Col>1 of 2 </Col>
<Col>2 of 2</Col>
</Row>
<Row>
<Col><Button variant="outline-primary" size="lg" block>QR Code
</Button></Col>
<Col><Button variant="outline-success" size="lg" block onClick={Survey} >Survey</Button></Col>
</Row>
<Row>
<Col>1 of 2</Col>
<Col>2 of 2</Col>
</Row>
<Row>
<Col><Button variant="outline-warning" size="lg" block>Warning</Button></Col>
<Col><Button variant="outline-secondary" size="lg" block>Secondary</Button></Col>
</Row>
<Row>
<Col>1 of 2</Col>
<Col>2 of 2</Col>
</Row>
</Container>
</div>
</div>
);
}
}
export default App;

onClick is an event handler, it's a function that is called to do something... there does not exist any built-in functionality in React that would allow you to give a component directly to a function and expect it to know what to do with it and when.
What you'd need to do is store a piece of data in your state that you can use to conditionally render the component.
I noticed you tagged this question with react-router. If you're talking about how to provide a component for a certain route, you need to import their built-in Route, Switch and Link components and provide your own component as the component prop on the Route. They provide an example.

I think you are misunderstanding for the onClick handler.
it requires function not react component or object.
Please use this.
import Survay from './survay'; // use correct path of the survay component
class App extends Component {
state = {
display: false,
}
render() {
return (
<div className="App">
<Container>
<Row>
<Col><ClientInformation /></Col>
</Row>
<Row>
<Col>1 of 2 </Col>
<Col>2 of 2</Col>
</Row>
<Row>
<Col><Button variant="outline-primary" size="lg" block>QR Code
</Button></Col>
<Col><Button variant="outline-success" size="lg" block onClick={()=>{this.setState({display: true})} >Survey</Button></Col>
</Row>
<Row>
<Col>1 of 2</Col>
<Col>2 of 2</Col>
</Row>
<Row>
<Col><Button variant="outline-warning" size="lg" block>Warning</Button></Col>
<Col><Button variant="outline-secondary" size="lg" block>Secondary</Button></Col>
</Row>
<Row>
<Col>1 of 2</Col>
<Col>2 of 2</Col>
</Row>
{
display &&
<Survey/>
}
</Container>
</div>
</div>
);
}
}
export default App;
And then please add the style to set a proper position and displaying for your Survay component.

Related

hidden-xs-down is not working React bootstrap

I am trying to hide few columns on mobile screens but its not working for some reason. Here's my code:
<Row>
<Col className="col-md-2">Route Image</Col>
<Col className="col-md-3">
<Row>
<Col>Route Name</Col>
</Row>
<Row>
<Col>Route Location</Col>
</Row>
</Col>
<Col className="col-md-3 hidden-xs-down">Route Difficulty</Col>
<Col className="col-md-2 hidden-xs-down">Route Length</Col>
<Col className="col-md-2 hidden-xs-down">View Details</Col>
</Row>
Can someone please tell me what I am doing wrong?
Since Bootstrap v4.x, all .hidden- classes have been removed. You should use d-none .d-sm-block instead if you want to hide your element only on xs

React suite error: component is not exported from 'rsuite'

I just found out about react suite and it's really good but I can't figure out this error that says component is not exported from rsuite per example 'Attempted import error: 'Card' is not exported from 'rsuite'.' and this is happening very often like where is the component if not from rsuite and the docs for it are really bad it doesn't help much
import "./App.css";
import "rsuite/dist/styles/rsuite-default.css";
import { Row, Col, Card } from "rsuite";
const styles = {
padding: 20,
textAlign: "center",
};
function App() {
return (
<div className='App' style={styles}>
<Row>
<Col md={6} sm={12}>
<Card />
</Col>
<Col md={6} sm={12}>
<Card />
</Col>
<Col md={6} sm={12}>
<Card />
</Col>
<Col md={6} sm={12}>
<Card />
</Col>
</Row>
</div>
);
}
export default App;
I can't find out why this is happening, tried looking up youtube, google or anything but it seems like the community for it is not that big I don't understand and I've had this with a lot of other components. Any help would be appreciated I really wanna use this library it looks really good
There is no Card component in the rsuite library, but you can implement it by combining Panel components.
https://rsuitejs.com/components/panel
In addition, the import of components is explained at the beginning of each component's documentation.

style div next to and on top of each others

so i have divs created in react basically they are components and i want to style them using bootstrap like this:
div div div
div div
div div
so basically there are 5components,the top 3 should be small and stack next to each others with small margin. the middle two are bigger and they are under the right and left divs and the bottom ones the same as the middle divs.
i have tried doing like col-6 for the middle and col-4 for the top ones but they were all over the place and messy.
this is what i tried without using css just bootstrap:
<div className="container">
<div className="row">
<div className="col-lg-4">
<Card />
<ActualLineChart data={systolic}/>
<ActualLineChart data={Diastolic}/>
</div>
</div>
<div className="row">
<div className="col-lg-6">
<div>
<ActualBarChart data={systolicAndDiastolicAndPulseAverageNew}/>
</div>
<ActualScatterChart data={systolicAndDiastolicAndPulseAverageNew}/>
</div>
</div>
<div className="row">
<div className="col-lg-6">
<DistributionChart/>
<DistributionChart />
</div>
</div>
</div>
how can i do this behavior?
I am guessing that if you are using react-bootstrap you probably have already installed it and included react component in your app.js file using this
import { Container, Row, Col } from 'React-Bootstrap';
Then you can simply use this for your desired output.
<Container>
<Row>
<Col xs="4">
component 1
</Col>
<Col xs="4">
component 2
</Col>
<Col xs="4">
component 3
</Col>
</Row>
<Row className="justify-content-between">
<Col xs="4">
component 4
</Col>
<Col xs="4">
component 5
</Col>
</Row>
<Row className="justify-content-between">
<Col xs="4">
component 6
</Col>
<Col xs="4">
component 7
</Col>
</Row>
</Container>
Output will be look like this:

dynamically add and delete input fields in react

I am creating a React form where user can upload an image. I am using a function createObjectURL() as given below which i set as the state.
This is the image component
<Row className="form-row">
<Col md={8} className="image-field">
<div className="input-image-file">
<img src={this.state.image} className="myimage" alt="choose an image"></img>
</div>
</Col>
<Col md={4} className="image-buttons">
<Row className="form-row">
<FormControl type="file" onChange={this.onImageChange.bind(this)} ></FormControl>
</Row>
<Row className="form-row">
<Button className="delete-image" onClick={this.deleteImage.bind(this)}><i className="fa fa-trash"></i> Delete</Button>
</Row>
</Col>
</Row>
this is the onImageChange and deleteImage function
onImageChange(event){
this.setState({image: URL.createObjectURL(event.target.files[0])},()=>{
console.log(this.state.image)
})
}
deleteImage(){
this.setState({image : null})
}
the URL is like
"blob:http://localhost:3000/e5531071-c8fc-46db-a934-98678c1cec3a"
I send this data to backend and create an entry in mongodb also where is the URL is saved correctly. I have an option called edit which retrieves all the contents of the form for the user to edit. All contents are getting displayed except the Image.
I get an error like
GET blob:http://localhost:3000/e5531071-c8fc-46db-a934-98678c1cec3a 404 (Not Found)
please do help me thanks in advance this is the console error log

Dynamic EventKey not working React-Bootstrap

I am dynamically rendering my tabs in React-Bootstrap, but somehow the eventKey doesn't recognise the values and the tabs aren't clickable.
Here's the code.
var tabButtons = tabsList.map(function(text, index){
if(text.enabled==1)
{
return <NavItem eventKey={index} key={index}><center> {text.value}</center></NavItem>; }
});
Here tabList is an array which contains information about whether the button is enabled. If yes, then display the button information.
The code where I am calling these tabs.
render() {
return (
<div>
<Tab.Container id="tab" defaultActiveKey="0">
<Row className="show-grid">
<Col sm={12}>
<Nav bsStyle="tabs" justified>
{tabButtons}
</Nav>
</Col>
<Col sm={12}>
<Tab.Content animation>
<Tab.Pane eventKey="0">
<FirstTabContent />
</Tab.Pane>
<Tab.Pane eventKey="1">
Second
</Tab.Pane>
<Tab.Pane eventKey="2">
<ThirdTabContent />
</Tab.Pane>
</Tab.Content>
</Col>
</Row>
</Tab.Container>
</div>
);
I have no idea, why aren't the eventKey able to use the index values.
Anything I am missing? Another approach perhaps?
Apparently after some hit and tries, got it working.
The problem was indeed in eventKey.
Replaced eventKey="0" with eventKey={0}
Though I have no idea what difference it makes. Anyone?

Resources