reactjs onclick display component - javascript

Why isn't my component rendering?
import React, { Component } from 'react';
import {Chokers, Bracelets, FRings, MRings} from './AllItems.js'
class App extends Component {
handleClick(e){
<Chokers chokers={this.props.items.Chokers}/>
}
render() {
return (
<div className="App">
<ul style={{display: 'inline'}}>
<li onClick={this.handleClick.bind(this)}>Chokers</li>
<li><Bracelets bracelets={this.props.items.Bracelets}/>Bracelets</li>
<li><FRings frings={this.props.items.FRings}/>Rings for Women</li>
<li><MRings mrings={this.props.items.MRings}/>Rings for Men</li>
</ul>
</div>
);
}
}
export default App;
Basically my this.props.items.Chokers for examples call a on my json file that was passed through.
I just want to create a links to another component at an onClick event.
The problem that I am having is that my Component under handleClick doesnt render.

If you want to render Chockers component on the click of item, then write it like this, create the state variable and set it true onClick of the item:
class App extends React.Component {
constructor(){
super();
this.state = {render:''}
}
handleClick(compName, e){
console.log(compName);
this.setState({render:compName});
}
_renderSubComp(){
switch(this.state.render){
case 'chockers': return <Chokers/>
case 'bracelets' : return <Bracelets/>
case 'rings': return <FRings/>
}
}
render() {
return (
<div className="App">
<ul style={{display: 'inline'}}>
<li onClick={this.handleClick.bind(this, 'chockers')}>Chokers</li>
<li onClick={this.handleClick.bind(this, 'bracelets')}>Bracelets</li>
<li onClick={this.handleClick.bind(this, 'rings')}>Rings for Women</li>
</ul>
{this._renderSubComp()}
</div>
);
}
}
class Chokers extends React.Component {
render(){
return <div>Inside Chockers</div>
}
}
class FRings extends React.Component {
render(){
return <div>Inside FRings</div>
}
}
class Bracelets extends React.Component {
render(){
return <div>Inside Bracelets</div>
}
}
ReactDOM.render(<App />, document.getElementById('container'));
Check the jsfiddle for working example: https://jsfiddle.net/ocg4ebdf/

You are not rendering anything.You just need to return the Chokers component from render function itself.
create some state variable and set that variable to true so that upon this variable check you can render the Chokers component.

Try to something like below.
import React, { Component } from 'react';
import {Chokers, Bracelets, FRings, MRings} from './AllItems.js'
class App extends Component {
var flag;
handleClick(e){
flag = true;
render() {
return (
<Chokers chokers={this.props.items.Chokers}/>
);}
}
if(!flag){
render() {
return (
<div className="App">
<ul style={{display: 'inline'}}>
<li onClick={this.handleClick.bind(this)}>Chokers</li>
<li><Bracelets bracelets={this.props.items.Bracelets}/>Bracelets</li>
<li><FRings frings={this.props.items.FRings}/>Rings for Women</li>
<li><MRings mrings={this.props.items.MRings}/>Rings for Men</li>
</ul>
</div>
);
}
}
}
export default App;

Related

How to call an event handler function from App.js in two other components

I created a reset function in App.js and want to call it by an onclick in two other components. the problem is that it works in one component but doesn't in the other.
Here are the codes snippets
App.js
import React from 'react';
import Result from './components/Result';
import GeneralResult from './components/GeneralResult';
class App extends Component {
constructor(props) {
super(props);
this.state = {
result: '',
counter: 0,
}
}
// Reset function
handleReset=()=>{
this.setState({
result: '',
counter: 0,
)}
renderResult() {
return (
<div>
<Result reset={()=>this.handleReset()} />
<GeneralResult back={()=>this.handleReset()} />
</div>
);
}
Result.js
first component making use of reset()
function Result(props) {
return (
<div>
<span>
<button onClick={props.reset}>Replay</button>
</span>
</div>
);
}
export default Result;
GeneralResult.js
second component making use of the reset
import React, { Component } from 'react';
export default class GeneralResult extends Component {
render() {
return (
<React.Fragment>
<h2>Congratulations you won!</h2>
<span>
<button onClick={props.back}> Back to Question</button>
</span>
</React.Fragment>
);
}
}
You can pass the handler as props, and render the component from the parent class.
class Child extends Component {
render(){
return(
<button onClick = {this.props.onClick}></button>
)
}
}
export default Child;
import Child from 'path/to/child';
class Parent extends Component {
onClick = (e) => {
//do something
}
render () {
return(
<Child onClick = {onCLick}/>
)
}
}
Problem is that GeneralResult is class based component. so when you need to access props passed to it. you have to use this.props.
export default class GeneralResult extends Component {
render() {
return (
<React.Fragment>
<h2>Congratulations you won!</h2>
<span>
// you need to change "props.back"
// to "this.props.back"
<button onClick={this.props.back}> Back to Question</button>
</span>
</React.Fragment>
);
}
}

React Loading New Page

I am trying to load a different React component using a button. It worked when doing it for authentication with GitHub using Firebase, but won't work for this page.
import React from 'react';
import './index.css';
import GamePage from '../Game';
class Home extends React.Component {
constructor(props){
super(props);
this.LoadGamePage = this.LoadGamePage.bind(this);
}
LoadGamePage() {
return(
<div>
<GamePage />
</div>
)
}
render(){
return(
<div className="home">
<h1>Home Page</h1>
<button onClick={this.LoadGamePage}>Play PIT</button>
</div>
)
}
}
export default Home;
Is there something wrong with my LoadGamePage function?
How it is supposed to work? You have an onclick handler, which calls a class method. That class method, called LoadGamePage, returns JSX. Okey, but what now? It is returned, but... not rendered. It won't display anywhere. What would I suggest you? Instead of returning the JSX inside that handler, I would set state and depending on state I would render the Game Page or not.
class Home extends React.Component {
constructor(props){
super(props);
this.state = {
gameVisible: false,
}
this.LoadGamePage = this.LoadGamePage.bind(this);
}
LoadGamePage() {
this.setState({ gameVisible: true });
}
render() {
if (this.state.gameVisible) {
return <GamePage />
}
return (
<div className="home">
<h1>Home Page</h1>
<button onClick={this.LoadGamePage}>Play PIT</button>
</div>
)
}
}

How to pass data from parent to child in react.js

I have a parent component which has 1 child. I am updating my child by passing data through props. initially, it works fine but when I click on a button and update the state using setState the child gets rendered with old values by the time setState is finished. I have solved it using componentWillReceiveProps in the child but is this the right way?
In the below code if I do setState in filterResults function it won't update the Emplist component .
import React, { Component } from 'react';
import {Search} from './search-bar'
import Emplist from './emplist'
class App extends Component {
constructor(props){
super(props);
this.emp=[{
name:'pawan',
age:12
},
{
name:'manish',
age : 11
}]
this.state={emp:this.emp};
this.filterResults=this.filterResults.bind(this);
}
filterResults(val)
{
if(this.state)
{
let filt=[];
filt.push(
this.emp.find(e=>{
return e.age==val
})
);
this.setState({emp:filt});
}
}
render() {
return (
<div className="App">
<Search filterResults={this.filterResults}/>
<Emplist emp={this.state.emp}/>
</div>
);
}
}
export default App;
EmpList Componet
import React,{Component} from 'react'
export default class Emp extends Component
{
constructor(props){
super(props);
this.emplist=this.props.emp.map(e=>{return <li>{e.name}</li>});
this.next=this.emplist;
}
componentWillReceiveProps(nextProps,nextState,prevProps,prevState,nextContext,prevContext){
// this.props.updated(this.props.empo);
this.next=nextProps.emp[0];
if(this.next)
this.emplist= nextProps.emp.map(e=>{return <li>{e.name}</li>});
}
render(){
if(!this.next)
return <div>name not found</div>
else
return (
<div>
<br/>
<p>The list is here</p>
<ul>
{this.emplist}
</ul>
</div>
)
}
}
If you want to pass from parent to child you can pass using props and if you wan t to do reverse than you can pass one function from parent to child and than use this passed function to send something back to parent.
child will look something like this
class Reciepe extends Component{
render(){
const { title, img, instructions } = this.props;
const ingredients=this.props.ingredients.map((ing,index)=>(<li key={index} >{ing}</li>));
return (
<div className='recipe-card'>
<div className='recipe-card-img'> <img src={img} alt={title}/> </div>
<div className='recipe-card-content'>
<h3 className='recipe-title'>Reciepe {title}</h3>
<ul> {ingredients} </ul>
<h4>Instructions:</h4>
<p>{instructions}</p>
</div>
</div>
)
}
}
parent will look something like this
class RecipeList extends Component{
render(){
return (
<div style={{'display':'flex'}}>
{this.props.recipes.map((item,index)=>(
<Recipe key={index}
title={item.title}
ingredients={item.ingredients}
instructions={item.instructions}
img={item.img}
/>
))}
</div>
)
}
}
The problem is that you are assigning the values to this which is not a good practice. Check where to declare variable in React here.
If you are not using the props to do any complex operations. This should work.
EmpList Componet
import React, {Component} from 'react'
export default class Emp extends Component {
constructor(props) {
super(props);
}
render() {
if (!this.next)
return <div>name not found</div>;
else
return (
<div>
<br/>
<p>The list is here</p>
<ul>
{this.props.emp && this.props.emp.map(e => <li>{e.name}</li>)}
</ul>
</div>
)
}
}
Your next and emplist class properties are directly derivable from your props and hence you don't actually need them. You could do it in the following way
import React,{Component} from 'react'
export default class Emp extends Component{
render(){
const { emp } = this.props;
if(!emp || emp.length === 1)
return <div>name not found</div>
else {
return (
<div>
<br/> <p>The list is here</p>
<ul>
{emp.map(e=>{return <li>{e.name}</li>});}
</ul>
</div>
)
}
}
}
However in cases when you do what to make really complex decisions based on props, a combination of componentWillReceiveProps and componentDidMount/componentWillMount is the right place to do it.

React slide in navigation on button click

I am starting to work with React and trying to get my head around it. I am currently trying to build a navigation component that slides in when a button is clicked (the button lives in another component).
Here is my code so far,
class Application extends React.Component {
constructor() {
super();
this.state = {
sidebarOpen:false
}
}
handleViewSidebar() {
this.state.sidebarOpen = !this.state.sidebarOpen;
}
render() {
return(
<div>
<Navigation isOpen={this.state.sidebarOpen} toggleSidebar={this.handViewSidebar}/>
<Header isOpen={this.state.sidebarOpen} />
</div>
);
}
}
class Header extends React.Component {
constructor(props) {
super(props);
this.slideMenu = this.slideMenu.bind(this);
}
render() {
return(
<header>
<div className="container">
<h1><a>AppName</a></h1>
<div className="user__actions">
<a>Notifications</a>
<a onClick={this.slideMenu}>Menu</a>
</div>
</div>
</header>
);
}
slideMenu() {
this.setState({sidebarOpen:true});
console.log(this.state);
}
}
class Navigation extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<nav className={(this.props.sidebarOpen ? "site__navigation visible" : "site__navigation")}>
<a>Friends</a>
<a>Matches</a>
<a>Messages</a>
<a>Profile</a>
<a>Search</a>
</nav>
)
}
}
/*
* Render the above component into the div#app
*/
React.render(<Application />, document.getElementById('app'));
What I am finding is that one state is passed through all my components? In my slideMenu function i console log this.state but it is null. I cannot work out how to add a class to Navigation componenet on click of button to make the nav bar visible?
class Application extends React.Component {
constructor() {
super();
this.state = {
sidebarOpen:false
}
}
handleViewSidebar() {
this.setState({sidebarOpen:!this.state.sidebarOpen});
}
render() {
return(
<div>
<Navigation isOpen={this.state.sidebarOpen} toggleSidebar={this.handleViewSidebar.bind(this)}/>
<Header isOpen={this.state.sidebarOpen} toggleSidebar={this.handleViewSidebar.bind(this)}/>
</div>
);
}
}
class Header extends React.Component {
render() {
return(
<header>
<div className="container">
<h1><a>AppName</a></h1>
<div className="user__actions">
<a>Notifications</a>
<a onClick={this.props.toggleSidebar}>Menu</a>
</div>
</div>
</header>
);
}
}
class Navigation extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<nav className={(this.props.isOpen === true ? "site__navigation visible" : "site__navigation")}>
<a>Friends</a>
<a>Matches</a>
<a>Messages</a>
<a>Profile</a>
<a>Search</a>
</nav>
)
}
}
This will work for you, there were little mistakes which i corrected for you
Your example code is not working mostly because of small mistakes such as :
Directly assigning state (which will not call render to update your application). You need to update your app through setState() calls.
handleViewSidebar() {
this.state.sidebarOpen = !this.state.sidebarOpen;
}
Should be
handleViewSidebar() {
this.setState({sidebarOpen: !this.state.sidebarOpen});
}
and passing props with different names but using them with the initial name. Example: sidebarOpen vs isOpen
You also don't need "slideMenu" as you can pass handleViewSidebar as a prop and call it directly from the Header component.

ReactJS: Can not type into input field

I am starting to learn react and download and follow any tutorials in internet. I am trying to build friend list.
I have tree components,
friends_container:
import React from 'react';
import AddFriend from './add_friend.jsx'
import ShowList from './show_list.jsx'
class FriendsContainer extends React.Component {
constructor() {
this.state = {
friends: ['Jake Lingwall', 'Murphy Randall', 'Merrick Christensen']
}
}
addFriend(friend) {
this.setState({
friends: this.state.friends.concat([friend])
});
}
render() {
return (
<div>
<h3> Add your friend to friendslist </h3>
<AddFriend addNew={this.addFriend}/>
<ShowList names={this.state.friends}/>
</div>
)
}
}
export default FriendsContainer;
add_friend:
import React from 'react';
class AddFriend extends React.Component {
constructor() {
this.state = {newFriend: ''};
}
updateNewFriend(e) {
this.setState({
newFriend: e.target.value
})
}
handleAddNew() {
this.props.addNew(this.state.newFriend);
this.setState({
newFriend: ''
});
}
render() {
return (
<div>
<input type="text" value={this.state.newFriend} onChange={this.updateNewFriend}/>
<button onClick={this.handleAddNew}>Add Friend</button>
</div>
)
}
}
AddFriend.propTypes = { addNew: React.PropTypes.func.isRequired };
export default AddFriend;
show_list:
import React from 'react';
class ShowList extends React.Component {
render() {
var listItems = this.props.names.map((f, i) => <li key={i}>{f}</li>);
return (
<div>
<h3>Friends</h3>
<ul>
{listItems}
</ul>
</div>
)
}
}
ShowList.defaultProps = { names: [] };
export default ShowList;
and app.jsx
import React from 'react';
import FriendsContainer from './components/friends_container.jsx';
window.React = React;
React.render(<FriendsContainer />, document.body);
as you can see on the code, I am using es6 and babel as transcompiler.
My problem, I can not type any letters into the input field to add new friend into friends list. What am I doing wrong?
In the context of your updateNewFriend method, this refers to the window object and not the current component instance. You need to bind the method before passing it as the onChange event handler. See Function::bind.
You have two options:
class AddFriend extends React.Component {
constructor() {
// ...
this.updateNewFriend = this.updateNewFriend.bind(this);
this.handleAddNew = this.handleAddNew.bind(this);
}
}
or
class AddFriend extends React.Component {
render() {
return (
<div>
<input type="text" value={this.state.newFriend} onChange={this.updateNewFriend.bind(this)}/>
<button onClick={this.handleAddNew.bind(this)}>Add Friend</button>
</div>
)
}
}
Keep in mind that Function::bind returns a new function, so binding in render creates a function every time your component is rendered, though the performance impact is negligible.

Categories