Value empty in react UI but visible in the console - reactjs

I was using react with flux. When I checked in the console, values are coming but when I check in React developer tools, expected value is empty. If I am sending a single object then it is working fine. But if an array is sent, then nothing is displayed.
Can anybody please tell what is the problem? The code and image of the same is as below.
Code:
var Product = React.createClass({
render: function(){
var product = this.props.product;
return (<div className="col-md-8"><hr/>
{product.map(function(items){
console.log(" Items in the field :"+JSON.stringify(items));
<div className="col-sm-6">
<div className="col-sm-4">
<img src={"/src/image/"+ items.image}/>
</div>
<div className="col-sm-2">
<h2> {items.name} </h2>
<p> {items.description} </p>
<h3> Price: ${items.price} </h3>
<h4>Inventory: {items.inventory}</h4>
</div>
</div>
})}
</div>
)
}
});

You need to return from function call
var Product = React.createClass({
render: function(){
var product = this.props.product;
return (<div className="col-md-8"><hr/>
{product.map(function(items){
console.log(" Items in the field :"+JSON.stringify(items));
return (<div className="col-sm-6">
<div className="col-sm-4">
<img src={"/src/image/"+ items.image}/>
</div>
<div className="col-sm-2">
<h2> {items.name} </h2>
<p> {items.description} </p>
<h3> Price: ${items.price} </h3>
<h4>Inventory: {items.inventory}</h4>
</div>
</div>);
})}
</div>
)
}
});

Related

Why mapped items states are affecting on each other in React?

I mapped an array of products, each of them have, I am managing state with redux, each mapped item has quantity if I increase quantity, price should also increase, so when I am changing quantity of one item second array price also changes based on previous item quantity, so why are different items affecting on each other? here is the code:
const handleProductQuantity = (event) => {
dispatch(setProductQuantity(event.target.value));
}
<div className='cart'>
<div className='products-in-cart-wrapper'>
{ productsInCart ? (
productsInCart.map(productInCart => (
<div className='product-in-cart'>
<div className="product-in-cart-name">
<div className="label label-title">
Title:
</div>
<div className="value value-title">
{productInCart.title}
</div>
</div>
<div className="product-in-cart-price">
<div className="label label-price">
Price:
</div>
<div className="value value-price">
{productQuantity * productInCart.price}
</div>
</div>
<div className="product-in-cart-price">
<div className="label label-price">
Quantity:
</div>
<div className="value value-quantity">
<input onChange={handleProductQuantity} type="number" placeholder='1'/>
</div>
</div>
<div className="button-cont">
<button className="buy-button" onClick={test}>
Buy now
</button>
</div>
</div>
))
) : (
<div> Loading... </div>
)
}
<div className="total-price-button-cont">
<button>Buy all for: 100$</button>
</div>
</div>
</div>
You should always use a key prop on items inside array. if you have an id by example, this would look like that :
<div className="product-in-cart" key={productInCart.id}>...</div>
React uses key to properly identify items inside the array. You problem might be different tho but you should always uses key
edit: productQuantity should be productInCart.productQuantity ;)

Aos animation does not work when i refresh the page

I'm working on a react.js project.Aos animation does not work when i refresh the page.It works when I open another page and come back.
import 'aos/dist/aos.css';
import AOS from 'aos';
function MenuCard(props) {
AOS.init();
AOS.refresh();
return (
<>
<div className="row">
<div className="col">
<div class="card bg-dark text-white" id="cardBody" data-aos="flip-left" data-aos-duration="800">
<img src={photos[`${props.id}`]} id="image" class="card-img" alt="..." />
<div class="card-img-overlay" id="cardImg">
<div id="baslik">
<a class="card-title" href={`#${props.title}`} id="cardTitle">{props.title}</a>
</div>
</div>
</div>
</div>
</div>
</>
)
}
I solved this issue with this code
document.addEventListener("DOMContentLoaded", function() {
setTimeout(function() { AOS.refresh(); }, 500);
});
it refreshes AOS after 0.5 seconds.

React JS Axios map

I have 4 components in my project, that have the APIs, and there information.
the App.js includes the APIs:
This is in my Country.js Component
render() {
const DataGroup = this.props.countries.map((county) => {
return <InfoData info={county} />;
});
const DataGroupAll = this.props.countriesAll.map((country) => {
return <InfosData infos={country} />;
});
return (
<div>
{DataGroup}
{DataGroupAll}
</div>
);
}
and the InfoData.js
<div class="card-columns">
<div class="card-body">
<h5 class="card-title">Date : {this.props.info.updated_at}</h5>
<p class="card-text">Confirmed : {this.props.info.today.confirmed}</p>
<p class="card-text">Deaths : {this.props.info.today.deaths}</p>
</div>
</div>
and the InfosData.js
<div class="card-deck">
<div class="card">
<img src={this.props.infos.flag} width="180" height="130" />
<div class="card-body">
<h4 class="card-title">Name : {this.props.infos.name}</h4>
<p class="card-text">Capital : {this.props.infos.capital}</p>
<p class="card-text">
Currency :
{this.props.infos.currencies.map((country) => {
return country.code;
})}
</p>
<p class="card-text">
Languages :
{this.props.infos.languages.map((country) => {
return country.name + ',';
})}
</p>
<p class="card-text">Timezones : {this.props.infos.timezones}</p>
</div>
</div>
</div>
but all the information I got will be in separate cards, how can I combine them by the country code ?

In react js this.state getting state of null in function

I want to change backgroundColor on hover but getting error when onMouseOver function get call it says "Uncaught TypeError: Cannot read property 'setState' of null" in console. Please let me know what is wrong with this code thanks.
import React from 'react'
import { Link } from 'react-router'
import { prefixLink } from 'gatsby-helpers'
import Helmet from 'react-helmet'
import { config } from 'config'
export default class Index extends React.Component {
constructor(props) {
super(props);
this.state = {
header: "harry header",
content: "content for harry",
activeClass : 'normal',
hovered :false
}
}
componentDidMount(){
var cssClass = 'normal';
window.addEventListener('scroll', (event) => {
if(event.srcElement.body.scrollTop == 0){
cssClass = 'normal';
}else{
cssClass = 'fixed';
}
this.setState({
activeClass: cssClass,
})
});
}
style() {
if (this.state.hovered) {
return { backgroundColor: "red" }
} else {
return { backgroundColor: "grey" }
}
}
onMouseOver (){
this.setState({ hovered:true });
}
onMouseOut (){
this.setState({ hovered:false });
}
render() {
return (
<div>
<header className={`header ${this.state.activeClass}`} >
<div className="top-bar">
<span>678-827-2782 </span>
<span> hellohello#knotel.com</span>
<button>Login</button>
</div>
</header>
<div className="banner-main-outer">
<div className="banner-main">
</div>
<div className="abs-height-caption">
<div className="caption-banner">
<div className="logo-main"><img src={require('../img/knotel.svg')}/></div>
<h2>Commit to your business, not a lease</h2>
<span>Headquarters as a Service</span>
<button>Book a tour</button>
</div>
<ul className="kno-ul">
<li><a> LOCATIONS</a></li>
<li><a> SPACES</a></li>
<li><a> Services</a></li>
<li><a> Events</a></li>
<li className="kno-hiring"><a> We're hiring</a></li>
</ul>
</div>
<div className="banner-btm-link">
<a>Tenant Reps</a>
<a>I have space</a>
</div>
</div>
<div className="Spaces-Offer">
<div className="header-bar">Our Locations</div>
<div className="map-outer">
<img src={require('../img/map.jpg')} />
</div>
</div>
<div className="our-offer">
<div className="header-bar">Spaces We Offer</div>
<div onMouseOver={this.onMouseOver}
onMouseOut={this.onMouseOut}
style={this.style()} className="offer-cards">
<ul>
<li className="blue-block">
<h1>Flexible Workspace For Growing Teams</h1>
<p>We provide the resources so growing teams can focus their business. Many of our companies have graduated from coworking spaces.</p>
<div className="offer-res-img" />
</li>
<li className="brown-block">
<h1>Branded Floors for Established Companies</h1>
<p>When you need your own floor we provide dedicated, branded space for your company with a backbone of Knotel management.</p>
<div className="offer-res-img" />
</li>
<li className="purple-block">
<h1>Flagship Buildings for World Leaders</h1>
<p>When you are ready to take over the world, Knotel will take over a building for you. Turnkey commercial real estate at any size.</p>
<div className="offer-res-img" />
</li>
</ul>
</div>
</div>
<div className="our-services">
<div className="header-bar">Services We Provide</div>
<div className="sources-cards">
<ul>
<li className="active">
<h1>Sourcing and Buildout</h1>
<p>Knotel bundles property market insight with interior design expertise and buildout management, which ensures the client’s space vision is met on time, on budget, and without headaches.</p>
<div className="sources-res-img" />
</li>
<li>
<h1>Seamless Operations</h1>
<p>Knotel provides comprehensive office services powered by domain experts and best of breed technology. Enjoy the highest ratio of conference rooms per person in the industry, and exclusive deals at a high quality set of partners</p>
<div className="sources-res-img" />
</li>
<li>
<h1>Flexible Terms</h1>
<p>Knotel has a simple pricing structure with flexible terms. We offer various month to month and fixed term agreements tailored to your needs.</p>
<div className="sources-res-img" />
</li>
<li>
<h1>Events and Community</h1>
<p>Knotels are vibrant spaces, with engaged communities and a strong roster of events. Enjoy office hours and informal access to like-minded industry professionals.</p>
<div className="sources-res-img" />
</li>
</ul>
<div className="source-img-change" />
</div>
</div>
<div className="our-events">
<div className="header-bar">Events we're hosting</div>
<div className="form-contact">
<div className="main-outer-inp">
<h1>Stay in the know about Knotel events and news</h1>
<div className="input-blocks">
<input placeholder="First Name (required)" type="text" />
</div>
<div className="input-blocks">
<input placeholder="Last Name (required)" type="text" />
</div>
<div className="input-blocks">
<input placeholder="Email (required)" type="text" />
</div>
</div>
<div className="sub-btn">
<button>SUBSCRIBE</button>
</div>
</div>
</div>
<div className="structure-events">
<div className="structure-container">
<div className="events-details">
<div className="events-knotels">COMING EVENTS</div>
<ul>
<li>
<div className="img-block"><img src={require('../img/manhattan-logo.jpg')} /></div>
<div className="eventdetail">
<div className="dates-on">
<span>07</span>
<p>Jun</p>
</div>
<div className="gtm-para">
<span>Entrepreneurship Council</span>
<p>8:30 am to 10:00 am at Knotel </p>
</div>
</div>
</li>
<li>
<div className="img-block"><img src={require('../img/Parrot Memento Knotel.png')} /></div>
<div className="eventdetail">
<div className="dates-on">
<span>14</span>
<p>Feb</p>
</div>
<div className="gtm-para">
<span>Knotel Private Founders Series</span>
<p>5:00 pm to 7:00 pm at Knotel Union Sq 1</p>
</div>
</div>
</li>
</ul>
</div>
<div className="events-details">
<div className="events-knotels">PAST EVENTS</div>
<ul>
<li>
<div className="img-block"><img src={require('../img/DLD2.jpg')} /></div>
<div className="eventdetail">
<div className="dates-on">
<span>12</span>
<p>May</p>
</div>
<div className="gtm-para">
<span>Future Of Cities - DLD NYC</span>
<p>8:00 am to 2:00 pm at Knotel Bryant </p>
</div>
</div>
</li>
<li>
<div className="img-block"><img src={require('../img/health.jpg')} /></div>
<div className="eventdetail">
<div className="dates-on">
<span>25</span>
<p>Apr</p>
</div>
<div className="gtm-para">
<span>Health Tech Founders' Stories</span>
<p>6:30 pm to 8:30 pm at Knotel Bryant</p>
</div>
</div>
</li>
<li>
<div className="img-block"><img src={require('../img/Space.jpg')} /></div>
<div className="eventdetail">
<div className="dates-on">
<span>20</span>
<p>Apr</p>
</div>
<div className="gtm-para">
<span>When Do We Go To Space?</span>
<p>6:30 pm to 8:30 pm at Knotel Bryant</p>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
<footer>
<div className="footer">
<div className="img-footer">
<img src={require('../img/knotel.svg')} />
</div>
<ul>
<li><a>Careers</a></li>
<li><a>Terms</a></li>
<li><a>Tenant Reps</a></li>
<li><a>I have space</a></li>
</ul>
<div className="Copyright-res">Copyright © 2017 KNOTEL. All rights reserved.</div>
</div>
</footer>
</div>
)
}
}
From the docs:
You have to be careful about the meaning of this in JSX callbacks. In JavaScript, class methods are not bound by default. If you forget to bind this.handleClick and pass it to onClick, this will be undefined when the function is actually called.
This is not React-specific behavior; it is a part of how functions work in JavaScript.
So you need to bind this to your methods to in order to make it work.
This is how your constructor should look:
constructor(props) {
super(props);
this.state = {
header: "harry header",
content: "content for harry",
activeClass: "normal",
hovered: false
}
this.onMouseOver = this.onMouseOver.bind(this)
this.onMouseOut = this.onMouseOut.bind(this)
}
You need to bind you class methods to this instance. One way to do that is in the constructor -
constructor(props) {
super(props);
this.onMouseOver = this.onMouseOver.bind(this)
this.onMouseOut = this.onMouseOut.bind(this)
this.state = {
header: "harry header",
content: "content for harry",
activeClass : 'normal',
hovered :false
}
}
Whenever you get an error which you can trace back to cannot read property x of null where null was supposed to be this, you know that the binding for this is not correct at the time the method/function was called.
The other answers are correct, but you can also add the events to the higher order <div>
so you can modify your component to be like
return (
<div onMouseOut={this.onMouseOut} onMouseOver={this.onMouseOver}>
...
</div>
);

Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `MovieResults`

I can not go to the Move.js page to page Details.js
I do not understand why I have this response : Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of MovieResults
Movie.js :
var Movie = React.createClass({
render: function(){
var link = 'http://www.imdb.com/title/' + this.props.movie.imdbID;
var myLink = 'search/' + this.props.movie.imdbID;
return(
<div className="well">
<div className="row">
<h4 className="text-center">
<Link to={myLink} activeClassName="current">{this.props.movie.Title}</Link>
</h4>
</div>
</div>
)
},
});
Details.js :
var Details = React.createClass({
render: function(){
var link = 'http://www.imdb.com/title/' + this.props.movie.imdbID;
var title = this.props.movie.Title;
var year = this.props.movie.Year;
var type = this.props.movie.Type;
var poster = this.props.movie.Poster;
var imdbID = this.props.movie.imdbID;
return(
<div className="well">
<div className="row">
<div className="col-md-4">
<img className="thumbnail" src={poster} />
</div>
<div className="col-md-8">
<h4><a href={this.props.movie.Title}> {title}</a></h4>
<ul className="padding">
<li className="list-group-item">Type : {type}</li>
<li className="list-group-item">Year Released : {year}</li>
<li className="list-group-item">Id imdb : {imdbID}</li>
</ul>
<a className="btn btn-primary" href={link}>View on IMDB</a>
</div>
</div>
<Movie movie={this.props.Details} key={i} />
</div>
)
},
});
DOCS
The key should always be supplied directly to the components in the
array, not to the container HTML child of each component in the array
So... Add key attribute to the first child <div>
var MovieResults = React.createClass({
render: function(){
return(
<div>
<h3 className="text-center"> Results </h3>
{
this.props.movies.map(function(movie, i){
return(
<div key={i}>
<Movie movie={movie}/>
</div>
)
})
}
</div>
)
}
})

Resources