I'm trying to put useState hook values to a component via props.
As I understand typescript complains that I didn't specify setActive to boolean type. Am I right? If so, how can I do it?
const Card = () => {
const [activeModal, setActiveModal] = useState<boolean>(false)
return (
<>
<ApproveModal active={activeModal} setActive={setActiveModal} />
<div className="card">
<div className="card-wrapper">
<div className="card-header">
<div className="card-header-left">
<img src={token} alt="token" />
<div className="card-header-info-tag">
<h1>WETH-WBNB LP</h1>
<p>Auto-Compounding</p>
</div>
</div>
<div className="card-header-right">
<p>Pancake</p>
<img src={pancake} alt="pancake" />
</div>
</div>
<div className="card-info">
<div className="card-info-item">
<div className="card-info-item-title">Staked</div>
<div className="card-info-item-value">$482.22K</div>
<div className="card-info-item-description">39595 LPs</div>
</div>
<div className="card-info-item">
<div className="card-info-item-title">APY</div>
<div className="card-info-item-value">365%</div>
<div className="card-info-item-description">Daily: 2.00%</div>
</div>
<div className="card-info-item">
<div className="card-info-item-title">TVL</div>
<div className="card-info-item-value">$482.22K</div>
<div className="card-info-item-description">39595 LPs</div>
</div>
</div>
<div className="card-button" onClick={() => setActiveModal(!activeModal)}>Approve</div>
</div>
</div>
</>
)
}
"text to fix little text problem"
"text to fix little text problem"
"text to fix little text problem"
And ApproveModal
interface ApproveModalProps {
active: boolean;
setActive: boolean;
}
const ApproveModal = ({active, setActive}: ApproveModalProps) => {
return (
<div className="modal">
<div className="modal-content">
<div className="modal-header">
<img src={modalLogo} alt="modalLogo" />
<div className="modal-header-info">
<div className="modal-header-info-title">WETH-WBNB LP</div>
<div className="modal-header-info-description">Auto-Compounding</div>
</div>
<div className="modal-header-info-swap">#PancakeSwap</div>
<img src={closeIcon} alt="closeIcon" className="close-button" />
</div>
<div className="modal-actions">
<div className="modal-actions-buttons">
<div className="deposit">Deposit</div>
<div className="withdrow">Withdrow</div>
</div>
</div>
<div className="modal-balance">
<div className="modal-balance-header">
<div className="modal-balance-header-left">
<div className="balance">Balance</div>
<div className="link">Create LP</div>
</div>
<div className="modal-balance-header-right">
<img src={updateIcon} alt="updateIcon" />
<div className="value">0.0000</div>
<div className="value-description">($0.00)</div>
</div>
</div>
<div className="balance-input">
<input type="text" />
<div className="input-button">MAX</div>
</div>
</div>
<div className="modal-info">
<div className="modal-info-apy">
<div className="modal-info-apy-title">APY</div>
<div className="modal-info-item">
<div className="apy-value">365%</div>
<div className="apr-value">APR: Swap 70.9% + GROW 73.5%</div>
</div>
</div>
<div className="modal-info-contact">
<div className="modal-info-contact-title">Contact</div>
<div className="modal-info-item">
<div className="contact-link">View on BScScan</div>
<div className="contact-address">oxAD6bD158869a97219447cf63b090</div>
</div>
</div>
<div className="modal-button">Approve</div>
<h1>No deposit free. No withdraw free.</h1>
</div>
</div>
</div>
)
}
Here is the code
The problem lies in your Card component. The error is saying that the setActive prop expects to receive a boolean value, but you are providing it with a function.
It should be expecting a function. You’ll want to change the props types on your Card such that setActive is a function whose argument is a boolean.
interface CardProps {
setActive: (active: boolean) => void;
…
If you want to be able to use a prevState callback when calling setActive inside the Card component, then you can use the exact type of your setActiveModal function, which is this:
interface CardProps {
setActive: React.Dispatch<React.SetStateAction<boolean>>;
…
Related
I was able to use the map method to display my static data in each container. There's a Modal template I created in order to provide more object/values info but if I click the button it opens all the modals of each containers at the same time because Modal tag wasn't assigned with unique index number when I mapped through my static data. Big brothers, please look at my codes and teach me how I can open only one associated Modal.
I created function Modal (template) and import to function Projects
function Modal({closeModal}) {
return (
<div className="modal-main">
<div className='modal-card'>
<img src="" />
<div className='modal-info'>
<div className='modal-title'>
<p className="title">title</p>
</div>
<div className='modal-body'>
<p className="tech">tech</p>
<p className="description">description</p>
<p className="github">github</p>
<p className="website">website</p>
</div >
<Button>detail</Button>
<Button onClick={()=> closeModal(false)}>close</Button>
</div>
</div>
</div>
)
}
export default Modal
function Projects() {
const [openModal, setOpenModal] = useState(false);
const [oneProject, setOneProject]
= useState(list_of_projects)
return (
<div className="section-header text-center mt-5">
<h4>projects</h4>
<p>list of proejcts</p>
<div className="proj-container">
{oneProject.map((item, index)=> {
return(
<div className='proj-map mx-2 my-2'key={index} >
<button type='button' className="btn btn-link " onClick={() => setOpenModal(true)}>
<div className=" proj-card card">
<img className='proj-img' src={item.thumbnail} style={{ width:300, height:300 }}/>
<div className="proj-text card-text">
<h3 className='title'>{item.title}</h3>
<h5 className='description'>description</h5>
<br/>
<h4>+</h4>
</div>
</div>
</button>
{openModal && <**Modal** closeModal={setOpenModal} /> }
</div>
)
})}
</div>
</div>
i konw there are similar question to this but i couldn't find the solution.
i'm trying to loop through this "div" with data coming from django rest api (JSON format)
async componentDidMount() {
const response = await fetch('/api/Post');
const data = await response.json();
this.setState({
posts: data.data[0],
loading: false
});
}
<div class="row">
<div class="col-md-6 col-6 paddding animate-box" data-animate-effect="fadeIn">
<div class="fh5co_suceefh5co_height_2"><img src={image} alt="img"/>
<div class="fh5co_suceefh5co_height_position_absolute"></div>
<div class="fh5co_suceefh5co_height_position_absolute_font_2">
<div class=""> <i class="fa fa-clock-o"></i> {date_posted} </div>
<div class=""> {title} </div>
</div>
</div>
</div></div>
i tried to use "map" but i'm not sure how
function renderposts() {
const postList = [];
for(let i = 0; i < this.state.posts.length; i++) {
let title = `${this.state.posts[i].title}`;
let image = this.state.posts[i].image;
let date_posted = this.state.posts[i].date_posted;
let key = this.state.posts[i].id.value;
postList.push(<Post
<div class="row">
<div class="col-md-6 col-6 paddding animate-box" data-animate-effect="fadeIn">
<div class="fh5co_suceefh5co_height_2"><img src={image} alt="img"/>
<div class="fh5co_suceefh5co_height_position_absolute"></div>
<div class="fh5co_suceefh5co_height_position_absolute_font_2">
<div class=""> <i class="fa fa-clock-o"></i> {date_posted} </div>
<div class=""> {title} </div>
</div>
</div>
</div></div>
/>);
}
return postList;
}
try like this in your render method:
{this.state.posts.map((post) => {
return (
<div
class="col-md-6 col-6 paddding animate-box"
data-animate-effect="fadeIn"
>
<div class="fh5co_suceefh5co_height_2">
<img src={post.image} alt="img" />
<div class="fh5co_suceefh5co_height_position_absolute"></div>
<div class="fh5co_suceefh5co_height_position_absolute_font_2">
<div class="">
<a href="#" class="color_fff">
{" "}
<i class="fa fa-clock-o"></i> {post.date_posted}{" "}
</a>
</div>
<div class="">
<a href="single.html" class="fh5co_good_font_2">
{" "}
{post.title}{" "}
</a>
</div>
</div>
</div>
</div>
);
})}
here is a codesandbox example using hooks:
codesandbox
I assume that data.data[0] is an array of data. In React, when you want to loop through a array of data, always use method that return a clone of your data such as map or filter to avoid problem with immutability. Here's the specific code for your problem:
const postList = this.state.posts.map((post) => {
// Destructuring assignment
const { title, image, date_posted } = post;
const key = post.id.value;
return (
<div class="row">
<div
class="col-md-6 col-6 paddding animate-box"
data-animate-effect="fadeIn"
>
<div class="fh5co_suceefh5co_height_2">
<img src={image} alt="img" />
<div class="fh5co_suceefh5co_height_position_absolute"></div>
<div class="fh5co_suceefh5co_height_position_absolute_font_2">
<div class="">
<a href="#" class="color_fff">
{' '}
<i class="fa fa-clock-o"></i> {date_posted}{' '}
</a>
</div>
<div class="">
<a href="single.html" class="fh5co_good_font_2">
{' '}
{title}{' '}
</a>
</div>
</div>
</div>
</div>
</div>
);
});
This question already has answers here:
Render is called twice when fetching data from a REST API
(3 answers)
Closed 2 years ago.
I try to get my pe list details on my single page when I click on the view details button but facing some issue so please help.
when i do {JSON.stringify(pet)} it gives a list of data proper but when I try to bind it's not showing any data but I check a network it gives proper data
my description.js component
import React, {useState, useEffect} from "react";
import {Link} from 'react-router-dom';
import Form from "./Form";
import Related from "./Related";
import WhySafari from "./WhySafari";
import {read} from "./apiCore";
const Description = (props) => {
const [pet, setPet] = useState({});
const [error, setError] = useState(false);
const loadsingelPet = id => {
read(id).then(data =>{
if(data.error){
setError(data.error);
}else{
setPet(data);
}
});
};
useEffect(() =>{
const id = props.match.params.id;
loadsingelPet(id);
}, [])
return(
<div>
<div className="bradcam_area breadcam_bg">
<div className="container">
<div className="row">
<div className="col-lg-12">
<div className="bradcam_text text-center">
<h3>{pet && pet.pbrd_display_name}</h3>
<ul>
<li><Link to="/">Home</Link> <i className="ti-angle-right"></i> </li>
<li><Link to="/list">Puppies for sale</Link> <i className="ti-angle-right"></i> </li>
<li><Link to="#">Golden Doodle</Link><i className="ti-angle-right"></i></li>
<li><Link to="#">German Shepherd</Link></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<section className="sample-text-area">
<div className="container">
<div className="row">
{/* {JSON.stringify(pet)} */}
<div className="col-lg-6">
<div className="gallery-container">
<div className="swiper-container gallery-main">
<div className="swiper-wrapper">
<div className="swiper-slide">
<Link to="img/puppy/1.png" data-fancybox="group1">
<img src={"http://localhost:8000/storage/uploads/puppies/"+pet.pet_image_ids} alt={pet.pet_image_ids}/>
</Link>
</div>
</div>
</div>
<div className="left-thumb">
<div className="swiper-container gallery-thumbs">
<div className="swiper-wrapper">
<div className="swiper-slide">
<img src="img/puppy/1.png" alt="Slide 01" />
</div>
<div className="swiper-slide">
<img src="img/puppy/1.png" alt="Slide 01" />
</div>
<div className="swiper-slide">
<img src="img/puppy/1.png" alt="Slide 01" />
</div>
</div>
</div>
<div className="swiper-button-prev"></div>
<div className="swiper-button-next"></div>
</div>
</div>
</div>
<div className="col-lg-6">
<div className="product-details">
<h1>{pet.pbrd_display_name}</h1>
<ul className="dtails-price">
<li className="real-price">$3449</li>
<li className="old-price">$4469</li>
</ul>
<div className="product-specification">
<h4><strong>Puppy Id : </strong> #656171</h4>
<h4><strong>Gender : </strong> {pet && pet.loc_contact_numbers }</h4>
<h4><strong>DOB : </strong> {pet && pet.plttr_birthdate }</h4>
<h4><strong>Location : </strong> {pet && pet.loc_receipt_name} </h4>
</div>
<div className="call-section">
<div className="call-left">
<img src="img/phone.svg" />
<h4>Need a nuppy guidience? <span>{pet && pet.loc_contact_numbers }</span></h4>
</div>
<div className="call-right">
<Link to="#" className="boxed-btn3">{pet && pet.pstatus_name }</Link>
</div>
</div>
<div className="decription-parra">
<h4>Description :</h4>
<p>{pet && pet.description}</p>
</div>
</div>
</div>
</div>
</div>
</section>
<Form/>
<WhySafari />
<Related />
</div>
);
}
export default Description;
please help. thanks in advance
Try useEffect like this:
useEffect(() =>{
const id = props.match.params.id;
loadsingelPet(id);
}, [props.match.params.id])
So it will re-run the effect if (props.match.params.id) changes and Ui will update too. React Documentation on hooks can be useful: https://reactjs.org/docs/hooks-effect.html
How can I pass the props from the ProductFeatures to the renderFeatures function ?
Below is a sample code:
const renderFeatures = (feature) => {
return (
<div key={feature.productFeatureTypeId} className="panel panel-default">
<div className="panel-heading">
<div className="row row-no-border row-h-10">
<div className="col-xs-10">
<a
className="accordion-toggle"
data-toggle="collapse"
href={"#" + feature.productFeatureTypeId}
>
<h4 className="panel-title">{feature.productFeatureTypeId}</h4>
</a>
</div>
<div className="col-xs-2 text-right font-size-16">
<span className="glyphicon glyphicon-circle-arrow-down"></span>
</div>
</div>
</div>
<div
id={feature.productFeatureTypeId}
className="panel-collapse collapse in"
>
<div className="panel-body">
{feature.productFeaturesDescription.map(renderFeatureObjItem)}
</div>
</div>
</div>
);
};
const ProductFeatures = (props) => {
let mappedProductFeaturesMembers = getMappedProductFeaturesMembers(
props.product.productFeatureMembers
);
return (
<div className="panel-group" id="accordion">
{mappedProductFeaturesMembers.map(renderFeatures)}
</div>
);
};
Note that all this code is inside one file named ProductFeatures.js and I am using functional components.
You can just pass it in the rednerFeatures like this:
const renderFeatures = (feature, props) => { // Accept both feature and props
return (
<div key={feature.productFeatureTypeId} className="panel panel-default">
<div className="panel-heading">
<div className="row row-no-border row-h-10">
<div className="col-xs-10">
<a
className="accordion-toggle"
data-toggle="collapse"
href={"#" + feature.productFeatureTypeId}
>
<h4 className="panel-title">{feature.productFeatureTypeId}</h4>
</a>
</div>
<div className="col-xs-2 text-right font-size-16">
<span className="glyphicon glyphicon-circle-arrow-down"></span>
</div>
</div>
</div>
<div
id={feature.productFeatureTypeId}
className="panel-collapse collapse in"
>
<div className="panel-body">
{feature.productFeaturesDescription.map(renderFeatureObjItem)}
</div>
</div>
</div>
);
};
const ProductFeatures = (props) => {
let mappedProductFeaturesMembers = getMappedProductFeaturesMembers(
props.product.productFeatureMembers
);
return (
<div className="panel-group" id="accordion">
{mappedProductFeaturesMembers.map(feature => renderFeatures(feature, props))} // Pass props here too
</div>
);
};
I am trying to make a component such that it holds another HTML element along with the React custom components. I created the component but it didn't work as I want. Currently, it didn't get rendered.
whole component structure
<AddingDetail>
<div className="row">
<DetailHeader link="/dashboard/setting" linkText="Back" heading="Add Role" />
<DetailBody>
<h1>Hello</h1>
</DetailBody>
<DetailFooter buttonText="Add" />
</div>
</AddingDetail>
AddingDetail component
render()
{
return(
<div className="col-md-12 bg-white border-radius-10">
</div>
)
}
DetailBody
render()
{
return(
<div className="col-md-12">
</div>
)
}
DetailHeader
return(
<div className="col-md-12 mgb-30" style={border} >
<div className="row" style={{marginBottom:'10px'}}>
<div className="col-md-6 flex vertical-center">
<h3 className="sub-heading roboto mgb-0">{this.props.heading}</h3>
</div>
<div className="col-md-6 align-right flex vertical-center flex-end">
<Link to={this.props.link}>
<button type="button" className="golden-button">{this.props.linkText}}</button>
</Link>
</div>
</div>
</div>
)
DetailFooter
return(
<div className="col-md-12 align-center mgb-20 mgt-20">
<button type="button" className="golden-button">{this.props.buttonText}</button>
</div>
)
You can use props.children to show the content in AddingDetail :
render() {
return(
<div className="col-md-12 bg-white border-radius-10">
{ this.props.children } //<---- HERE
</div>
)
}
And the same change you can apply to DetailBody.
render() {
return(
<div className="col-md-12">
{ this.props.children }
</div>
)
}