React, Link to component wont load until page is refreshed - reactjs

React, Link to component wont load until page is refreshed
My Link for terms and conditions won't load until the page is refreshed view the video below here.
https://youtu.be/2QumWBRXiuM
Here is the Footer.js code
The link to the page should be on line 44 I have it in a link tag than a list (li) tag.
import React from "react";
// import { Button } from "./Button";
import "./Footer.css";
import { Text, Div, Button, Row, Col, Container, Image } from "atomize";
import logo from "../logo/tryb_logo_medium.png";
import { Link } from "react-router-dom";
import TwitterIcon from '#material-ui/icons/Twitter';
import FacebookIcon from '#material-ui/icons/Facebook';
import InstagramIcon from '#material-ui/icons/Instagram';
import { Icon } from "#material-ui/core";
function Footer() {
return (
<div className="main-footer" id="onTop">
<Container>
<Row d="flex">
{/* Col 1
<div className="col-sm">
<section className="footer-subscription">
<p className="footer-subscription-heading">Join the Tryb</p>
<div className="input-areas">
<form>
<input
type="email"
name="email"
placeholder="Your email"
className="footer-input"
/>
</form>
<p className="footer-subscription-text">
Unsubscribe at any time
</p>
<Button>Subscribe</Button>
</div>
</section>
</div> *}
{/* Col 2 */}
<Col textColor="white" size={{ xs: '12', md: '4' }}>
<div text-align="center" p="1rem">
<h4>Need Help?</h4>
<ul className="list-unstyled">
<li>Contact Us</li>
<Link to="/termsandconditions">
<li>Terms & Conditions</li>
</Link>
</ul>
</div>
</Col>
{/* Col 4 */}
<Col size={{ xs: '12', md: '4' }} align="center">
<div p="1rem">
<h4>Social</h4>
<ul className="list-unstyled">
<a href="https://www.facebook.com/trybprints" target="_blank" ><FacebookIcon/></a>
<a href="https://www.instagram.com/tryb_prints/" target="_blank" ><InstagramIcon/></a>
<a href="https://twitter.com/PrintsTryb" target="_blank" ><TwitterIcon/></a>
</ul>
</div>
</Col>
{/* Col 3 */}
<Col size={{ xs: '12', md: '4' }}>
<div p="1rem">
<ul className="list-unstyled">
<li> <img
src={logo}
alt="tryb logo"
width="300px"
className="d-inline-block align-top"
/></li>
<li></li>
</ul>
</div>
</Col>
</Row>
</Container>
<hr />
<div className="copyright">
Copyright © Tryb Prints {new Date().getFullYear()}
</div>
</div>
);
};
export default Footer;
any help at all is greatly appreciated thanks

I can't recreate the problem, and would need to see more code to be more help (would like to see inside the <Switch>...</Switch> and inside the "TermsAndConditions" component).
You can maybe try to use useHistory? Something like this:
import { Link, useHistory } from "react-router-dom";
// ...
function Footer() {
const history = useHistory();
const handleClick = () => {
history.push("/termsandconditions");
}
return (
//...
// Remove the <Link> here
<a onClick={handleClick}>
<li>Terms & Conditions</li>
</a>
//...

Related

Follow link when pressing enter

I am using the react-router to pass input data from a search box when the user clicks the search button. However, when I press enter the input doesn't get passed over and the page just refreshes. Is there anyway I could set it so when the enter key is pressed the Nav routing happens?
import React, {useState} from 'react'
import '../sass/custom.scss';
import {Nav, Form, FormControl} from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
import {NavLink} from 'react-router-dom';
export const SearchBar = () => {
let [query, setQuery] = useState("")
return (
<div className="search-container">
<div className="row height d-flex justify-content-center align-items-center">
<div className="col-xs-12 col-sm-12 col-md-8 col-lg-8 my-5">
<div className="search"> <i className="fa fa-search"></i>
<Form className='d-flex mx-auto'>
<FormControl
type='search'
placeholder='Search'
className='form-control'
aria-label='Search'
onChange={e => setQuery(e.target.value) }
/>
<Nav.Link
as={NavLink}
exact={true}
to={{pathname:`/search`, state: `${query}` }}
className="search-button center btn btn-primary"
>
Search
</Nav.Link>
</Form>
</div>
</div>
</div>
</div>
)
}
Might not be the best solution, but you may give it a try
Just add onKeyPress on FormControl to track "enter key" and add preventDefault on form tag to prevent your page from reloading.
import React, { useState } from "react";
import '../sass/custom.scss';
import { Nav, Form, FormControl } from "react-bootstrap";
import 'bootstrap/dist/css/bootstrap.min.css';
import { NavLink, useHistory } from "react-router-dom";
export const SearchBar = () => {
let [query, setQuery] = useState("");
const history = useHistory();
return (
<div className="search-container">
<div className="row height d-flex justify-content-center align-items-center">
<div className="col-xs-12 col-sm-12 col-md-8 col-lg-8 my-5">
<div className="search">
<i className="fa fa-search"></i>
<Form
className="d-flex mx-auto"
onSubmit={(e) => e.preventDefault()}
>
<FormControl
type="search"
placeholder="Search"
className="form-control"
aria-label="Search"
onChange={(e) => setQuery(e.target.value)}
onKeyPress={(e: any) => {
if (e.nativeEvent.charCode === 13) {
history.push({ pathname: `/search`, state: `${query}` });
}
}}
/>
<Nav.Link
as={NavLink}
exact={true}
to={{ pathname: `/search`, state: `${query}` }}
className="search-button center btn btn-primary"
>
Search
</Nav.Link>
</Form>
</div>
</div>
</div>
</div>
);
};

React-Bootstrap Badge is only white text no matter how I change its property. How to solve it?

I am new to react. I have React app where I try to render a badge. It shows the text but only white.
I imported react-bootstrap with the following in my index.js
import 'bootstrap/dist/css/bootstrap.min.css';
in app.js I have
import Badge from "react-bootstrap/Badge";
Everything is working fine but the text is white.
Here is the Badge
<h4>
<Badge className="text-align-center" variant="secondary">
Editor
</Badge>
</h4>
Badge and text are not visible as below
If I select the text I can see there is a white text where the badge should be. I'm building a markdown previewer, everything is working fine both inside the editor and previewer.
Here is the full code: index.js
import React from 'react';
import ReactDOM from 'react-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();
app.js
import React from 'react';
import Badge from "react-bootstrap/Badge";
let marked = require("marked");
export default class App extends React.Component {
constructor(props){
super(props);
this.state = {
markdown: placeholder,
};
}
updateMarkdown(markdown){
this.setState({markdown });
}
render(){
var inputStyle = {
width: "500px",
height: "60vh",
marginLeft: "auto",
marginRight: "auto",
padding: "10px"
};
var outputStyle = {
width: "auto",
height: "auto",
backgroundColor: "#DCDCDC",
marginLeft: "auto",
padding: "10px"
};
return (
<div className="App">
<div className="container">
<div className="row mt-4">
<div className="col text-center">
<h1>
{" "}
<Badge className="text-align-center" variant="light">
Markdown Previewer
</Badge>
</h1>
</div>
</div>
<div className="row mt-4">
<div className="col-md-6">
{" "}
<div className="col text-center">
<h4>
<Badge className="text-align-center" bg="secondary" text="dark">
Editor
</Badge>
</h4>
</div>
<div className="input" style={inputStyle}>
<textarea
id="editor"
className="input"
style={inputStyle}
value={this.state.markdown}
onChange={(e) => {
this.updateMarkdown(e.target.value);
}}
>
</textarea>
</div>
</div>
<div className="col-md-6">
{" "}
<div className="col text-center">
<h4>
<Badge className="text-align-center" variant="secondary" id="preview" >
Previewer
</Badge>
</h4>
</div>
<div style={outputStyle}
dangerouslySetInnerHTML={{ __html: marked(this.state.markdown)}}></div>
</div>
</div>
</div>
</div>
);
}
}
'
const placeholder = '# Welcome to my React Markdown Previewer!'
I'm a little bit confused about your question so I'm trying to answer based on what I've assumed so far.
In react-bootstrap Badge there are 2 properties:
bg for The visual style of the badge
text for Setting badge text colour
Here you go!
If you just want to change the style of the text then use text property and if you want to change the style of your badge then use bg property
<h4>
<Badge className="text-align-center" bg="secondary" text="dark">
Editor
</Badge>
</h4>
I hope I've answered your question but if you still have a query, Checkout this link: https://react-bootstrap.github.io/components/badge/#badge-props

related to React js (props) css inline style

I want to more than one cards in different background color what i do ?
eg:
import React from "react";
import reactDom from "react-dom";
const BgColor=(props)=>{
return(
<>
<div className="main_div">
<div className="cards" >
<div className="card-side front" >
<div><span>C</span>opy<span>C</span>ode</div>
</div>
<div className="card-side back">
<div><i className="fas fa-copy fs-3" /><br />{props.code}</div>
</div>
</div>
</div>
</>
)
}
export default BgColor;
by pass value in app.js
and so on.....
please help me how to pass value inside the app.js
with this example is hard to to help you. Indeed, we don't know what your props object contains.
Let's assume your props contains
{
name: "toto",
code: "myCode"
}
In order to change background color based on props, you have multiples choices.
You can use dynamics classNames based on your props:
const BgColor=(props)=>{
return(
<>
<div className="main_div">
<div className="cards" >
<div className={`${props.name} card-side front`} >
<div><span>C</span>opy<span>C</span>ode</div>
</div>
<div className="card-side back">
<div><i className="fas fa-copy fs-3" /><br />{props.code}</div>
</div>
</div>
</div>
</>
)
}
See the <div className={${props.name} card-side front} > line. Go to css or scss file for this component and add a class selector inside of it.
In our example, it will be
.toto {
background: red
}
The second option is to inject directly the css inside your div.
Let's assume you send the desired background color inside your propos:
{
backgroundColor: "red",
code: "myCode"
}
In order to that, you have to do this
const BgColor=(props)=>{
return(
<>
<div className="main_div">
<div className="cards" >
<div className="card-side front" style={{backgroundColor: props.backgroundColor}}>
<div><span>C</span>opy<span>C</span>ode</div>
</div>
<div className="card-side back">
<div><i className="fas fa-copy fs-3" /><br />{props.code}</div>
</div>
</div>
</div>
</>
)
}
If you want to know more about css in React, you can check:
https://reactjs.org/docs/faq-styling.html, React.js inline style best practices, https://create-react-app.dev/docs/adding-a-stylesheet/
UPDATE
You can send an array of colors like <BgColor colors={["red", "blue"]} /> then in your BgColor component, you can map on your props like:
const BgColor=({colors})=>{
return (
<div className="main_div">
<div className="cards">
{colors.map((color, index) => {
return (
<div
className="card-side front"
style={{ backgroundColor: color }}
key={index}
>
<div>
<span>C</span>opy<span>C</span>ode
</div>
</div>
);
})}
</div>
</div>
);
}

showing the applicants of a job in reactjs using id

Iam trying to make a react application whereby users are applying for a job and also creating a report that will show the profile of the users who have applied for the job. Iam welcome to any ideas that can be given to me since I would like to have a logic on how to implement that.
Here is my code below:
DriverCard.js
import React from "react";
import member2 from "../../../dist/images/user2.png";
const DriverCard = ({cardValues}) => {
return (
<>
<div className="col-lg-3 col-md-3 col-sm-4 col-6 p-0 mb-5">
<div className="text-center">
<div className="mb-2">
<img
src={cardValues ? `${cardValues.location.title}` : member2}
className="rounded-circle"
height="85px"
width="85px"
alt="members"
/>
</div>
<h6 className="mb-0 textHeading">
{cardValues ? cardValues.name : `John Doe`}
</h6>
<span className="text-muted textStyle"> #JohntheD 5h</span>
<hr
style={{
width: "50%",
marginTop: 4,
marginBottom: 1,
}}
/>
<h6 className="textStyle mt-1">
{cardValues ? `${cardValues.licenseAge} +years licence` : `NA`}
</h6>
<h6 className="textStyle">
{cardValues ? `${cardValues.experience} +years experience` : `NA`}
</h6>
</div>
</div>
</>
);
};
export default DriverCard;
This is the report file that is suppossed to show the jobcard and the profile for the applicants.
import React, { useEffect, useState } from "react";
import {useHistory} from "react-router-dom";
import map from "lodash/map"
import { getSpecificJobCard } from "../../Api/index";
import arrowBack from "../../dist/images/arrowBack.svg";
import DriverCard from "./Job_Components/DriverCard";
import JobCardDetails from "./Job_Components/JobCardDetails";
import NextPageButtons from "../Main/DashBoard_Buttons/NextPageButtons";
const JobDetails = ({jobpostId}) => {
const [jobCard, setjobCard] = useState([]);
const history = useHistory();
console.log("param val",jobpostId);
useEffect(() => {
(async () => {
let jobCardRespData = await getSpecificJobCard(jobpostId);
setjobCard(jobCardRespData);
console.log("jobcard response:", jobCardRespData);
})();
}, []);
console.log("resp list ----- ", jobCard);
return (
<div className="">
<div className="col-md-10 m-auto">
<span style={{ cursor: "pointer" }} className="">
<img src={arrowBack} alt="" className="img-fluid" />
</span>
<div className="row">
<div className="col-lg-4 mt-5">
<div className="row">
{}
<JobCardDetails job={jobCard} />
</div>
</div>
<div className="col-lg-8 mt-5">
<div className="row">
<DriverCard/>
{/* <DriverCard /> */}
</div>
</div>
</div>
<div className="row">
<div className="col-lg-12">
<div className="row">
</div>
</div>
</div>
</div>
<div className="col-lg-12 my-3 border-top">
<NextPageButtons/>
</div>
</div>
);
};
export default JobDetails;

I had a question about passing id into my route of details when clicked

So once I click on the details page I would like to pass the id of the product to the url when you click it. So when i click the details page I would like for it to be myurl/details/itemid i found this StackOverflow answer but i cant seem to get it to work. React Router Pass Param to Component.
I would like for when my details page reloads it reloads withe correct items id.
this is my details page
import React, { Component } from "react";
import { ProductConsumer } from "../context";
import { Link } from "react-router-dom";
import { ButtonContainer } from "./Button";
import DropDown from "./Dropdown";
import ItemCategory from "./ItemCategory";
import { Carousel } from "react-responsive-carousel";
import "react-responsive-carousel/lib/styles/carousel.min.css";
import { AwesomeButton } from "react-awesome-button";
export default class Details extends Component {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
dropdownOpen: false
};
}
toggle() {
this.setState(prevState => ({
dropdownOpen: !prevState.dropdownOpen
}));
}
render() {
return (
return (
<div className="container-fluid width-100 bg-white py-5 mt-5 ">
{/* ProductInfo */}
<div className="row">
<div className="col mx-auto col-md-6 my-3 ">
<Carousel autoPlay>
<div>
<img src={img} className="img-fluid" alt="product" />
</div>
<div>
<img src={img2} className="img-fluid" alt="product" />
</div>
<div>
<img src={img3} className="img-fluid" alt="product" />
</div>
<div>
<img src={img4} className="img-fluid" alt="product" />
</div>
</Carousel>
{/* Add a Second Image */}
</div>
{/* Product Text */}
<div className="col mx-auto col-md-6 my-3 text-capitalize">
<h1 className="display-3">{title}</h1>
<h4 className="text-black">
<strong className="text-black">
price : <span>$</span>
{price}
</strong>
</h4>
<h4 className="text-blue">
</h4>
<p className="text-black ">{info}</p>
<p className="text-black ">{fabric}</p>
<small className="text-danger">{luxury}</small>
{/* buttons */}
<div>
<Link to="/all">
<AwesomeButton
className="text-capitalize mx-10"
ripple
size="large"
type="primary"
>
Back To Products
</AwesomeButton>
</Link>
<div className="mt-2">
<AwesomeButton
className="text-capitalize m-auto"
ripple
size="medium"
type="primary"
cart
disabled={inCart ? true : false}
onPress={() => {
value.addToCart(id);
}}
>
{inCart ? "inCart" : "add to cart"}
</AwesomeButton>
</div>
<ItemCategory title={category} />
<div className="mt-2">
<img
src="https://www.paypalobjects.com/digitalassets/c/website/marketing/na/us/logo-center/9_bdg_secured_by_pp_2line.png"
border="0"
alt="Secured by PayPal"
/>
</div>
</div>
</div>
</div>
</div>
);
}}
</ProductConsumer>
);
}
}
<Route path="/details/:id" component={Details} />
and in the component Details you have access
export default class Details extends Component {
render() {
return(
<div>
<h2>{this.props.match.params.id}</h2>
</div>
)
}
}

Resources