How to update state in map function in reactjs - reactjs

I am having 4 buttons each button have name id and selected boolean flag.
What I am trying to achieve is, on click of button, boolean button flag should be changed of that particular button. For this, I need to setState in map function for that particular button Id.
My issue is I am unable to setState in map function for that particular clicked button, its btnSelected should be changed
My aim is to create a multi-select deselect button.Its kind of interest selection for the user and based on that reflect the UI as well my array. Here is my code.
Thanks in anticipation.
import React, { Component } from "react";
import { Redirect } from "react-router-dom";
export default class Test extends Component {
constructor(props, context) {
super(props, context);
this.handleChange = this.handleChange.bind(this);
this.state = {
value: "",
numbers: [1, 2, 3, 4, 5],
posts: [
{
id: 1,
topic: "Animal",
btnSelected: false
},
{
id: 2,
topic: "Food",
btnSelected: false
},
{
id: 3,
topic: "Planet",
btnSelected: false
},
{ id: 4, topic: "Nature", btnSelected: false }
],
allInterest: []
};
}
handleChange(e) {
//console.log(e.target.value);
const name = e.target.name;
const value = e.target.value;
this.setState({ [name]: value });
}
getInterest(id) {
this.state.posts.map(post => {
if (id === post.id) {
//How to setState of post only btnSelected should change
}
});
console.log(this.state.allInterest);
if (this.state.allInterest.length > 0) {
console.log("Yes we exits");
} else {
console.log(id);
this.setState(
{
allInterest: this.state.allInterest.concat(id)
},
function() {
console.log(this.state);
}
);
}
}
render() {
return (
<div>
{this.state.posts.map((posts, index) => (
<li
key={"tab" + index}
class="btn btn-default"
onClick={() => this.getInterest(posts.id)}
>
{posts.topic}
<Glyphicon
glyph={posts.btnSelected === true ? "ok-sign" : "remove-circle"}
/>
</li>
))}
</div>
);
}
}

Here's how you do something like this:
class App extends Component {
state = {
posts: [{
name: 'cat',
selected: false,
}, {
name: 'dog',
selected: false
}]
}
handleClick = (e) => {
const { posts } = this.state;
const { id } = e.target;
posts[id].selected = !this.state.posts[id].selected
this.setState({ posts })
}
render() {
return (
<div>
<form>
{this.state.posts.map((p, i) => {
return (
<div>
<label>{p.name}</label>
<input type="radio" id={i} key={i} checked={p.selected} onClick={this.handleClick} />
</div>
)
})}
</form>
</div>
);
}
}
render(<App />, document.getElementById('root'));
Working example here.

You can do this by passing the index from the map into each button's handleClick function, which would then return another function that can be triggered by an onClick event.
In contrast to Colin Ricardo's answer, this approach avoids adding an id prop onto each child of the map function that is only used for determining the index in the handleClick. I've modified Colin's example here to show the comparison. Notice the event parameter is no longer necessary.
class App extends Component {
state = {
posts: [{
name: 'cat',
selected: false,
}, {
name: 'dog',
selected: false
}]
}
handleClick = (index) => () => {
const { posts } = this.state;
posts[index].selected = !this.state.posts[index].selected
this.setState({ posts })
}
render() {
return (
<div>
<form>
{this.state.posts.map((p, i) => {
return (
<div>
<label>{p.name}</label>
<input type="checkbox" key={i} checked={p.selected} onClick={this.handleClick(i)} />
</div>
)
})}
</form>
</div>
);
}
}
render(<App />, document.getElementById('root'));
Working example here

Related

change text of a specific button when clicked in React

I want to change the text of a specific button when I click on that button in React. But the issue is when I click the button the title will change for all buttons!
class Results extends Component {
constructor() {
super();
this.state = {
title: "Add to watchlist"
}
}
changeTitle = () => {
this.setState({ title: "Added" });
};
render() {
return (
<div className='results'>
{
this.props.movies.map((movie, index) => {
return (
<div className='card wrapper' key={index}>
<button className='watchListButton' onClick={this.changeTitle}>{this.state.title}</button>
</div>
)
})
}
</div>
)
}
}
You would need to come up with a mechanism to track added/removed titles per movie. For that, you would have to set your state properly. Example:
this.state = {
movies: [
{id: 1, title: 'Casino', added: false},
{id: 2, title: 'Goodfellas', added: false}
]
This way you can track what's added and what's not by passing the movie id to the function that marks movies as Added/Removed. I have put together this basic Sandbox for you to get you going in the right direction:
https://codesandbox.io/s/keen-moon-9dct9?file=/src/App.js
And here is the code for future reference:
import React, { Component } from "react";
import "./styles.css";
class App extends Component {
constructor() {
super();
this.state = {
movies: [
{ id: 1, title: "Casino", added: false },
{ id: 2, title: "Goodfellas", added: false }
]
};
}
changeTitle = (id) => {
this.setState(
this.state.movies.map((item) => {
if (item.id === id) item.added = !item.added;
return item;
})
);
};
render() {
const { movies } = this.state;
return (
<div className="results">
{movies.map((movie, index) => {
return (
<div className="card wrapper" key={index}>
{movie.title}
<button
className="watchListButton"
onClick={() => this.changeTitle(movie.id)}
>
{movie.added ? "Remove" : "Add"}
</button>
</div>
);
})}
</div>
);
}
}
export default App;

Error: Objects are not valid as a React child (found: object with keys {id, name})

I'm encountering an issue while compiling my reactJS component:
import React, { Component } from "react";
class StatefullComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [
{ id: 1, name: "Perceuse" },
{ id: 2, name: "disqueuse" },
{ id: 3, name: "marteau" },
{ id: 4, name: "clé de 17" },
{ id: 5, name: "meuleuse" }
],
newItem: ""
};
this.addNewItemToState = this.addNewItemToState.bind(this);
this.handleChange = this.handleChange.bind(this);
this.removeThisItem = this.removeThisItem.bind(this);
}
sortItems() {
this.setState({
items: this.state.items.sort()
});
}
addNewItemToState() {
this.setState({
items: [...this.state.items, this.state.newItem],
newItem: ""
});
}
removeThisItem(bidule) {
const tmparray = this.state.items.filter(bazar => {
return bazar.id !== bidule;
});
console.log(tmparray);
this.setState({
items: tmparray
});
}
handleChange(e) {
this.setState({
newItem: e.target.value
});
}
render() {
return (
<div className="statefull">
<h2>Satefull component, build with a class</h2>
<ul>
{this.state.items.map(item => (
<li className="lambda">
{item}
<button onClick={this.removeThisItem(item.id)}>
supprimer cet item
</button>
</li>
))}
</ul>
<input
type="text"
placeholder="Ajouter un element"
value={this.state.newItem}
onChange={this.handleChange}
/>
<button onClick={this.addNewItemToState}>Ajouter</button>
</div>
);
}
}
export default StatefullComponent;
and my App.js:
import "./App.scss";
import React from "react";
import StatelessComponent from "./components/stateless-component";
import StatefullComponent from "./components/statefull-component";
function App() {
return (
<div className="App">
<StatefullComponent />
</div>
);
}
export default App;
Why do I get this error?
'Error: Objects are not valid as a React child (found: object with keys {id, name}). If you meant to render a collection of children, use an array instead.'
Did I badly formatted my items array in my state? It's important to me to have an id because I want to be able to delete an item.
Any clue? Thanks.
That's because you are trying to render an object in map. Change your ul to this:
<ul>
{
this.state.items.map((item, index) => <li className="lambda" key={index}>{item.name}<button onClick={this.removeThisItem(item.id)}>supprimer cet item</button></li>)
}
</ul>
Hope this works for you.
You need to display name using {item.name} but not using {item}. This is reason you get that error.
Also I noticed the following issues:
1-) You need to add a key property to li tag like this: <li className="lambda" key={item.id}>. React needs key property in a list.
2-) You need to change onClick={this.removeThisItem(item.id)} to <button onClick={() => this.removeThisItem(item.id)}>. You are calling the removeThisItem method, but it shouldn't be called.
import React, { Component } from "react";
class StatefullComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [
{ id: 1, name: "Perceuse" },
{ id: 2, name: "disqueuse" },
{ id: 3, name: "marteau" },
{ id: 4, name: "clé de 17" },
{ id: 5, name: "meuleuse" }
],
newItem: ""
};
this.addNewItemToState = this.addNewItemToState.bind(this);
this.handleChange = this.handleChange.bind(this);
this.removeThisItem = this.removeThisItem.bind(this);
}
sortItems() {
this.setState({
items: this.state.items.sort()
});
}
addNewItemToState() {
this.setState({
items: [...this.state.items, this.state.newItem],
newItem: ""
});
}
removeThisItem(bidule) {
const tmparray = this.state.items.filter(bazar => {
return bazar.id !== bidule;
});
console.log(tmparray);
this.setState({
items: tmparray
});
}
handleChange(e) {
this.setState({
newItem: e.target.value
});
}
render() {
return (
<div className="statefull">
<h2>Satefull component, build with a class</h2>
<ul>
{this.state.items.map(item => (
<li className="lambda" key={item.id}>
{item.name}
<button onClick={() => this.removeThisItem(item.id)}>
supprimer cet item
</button>
</li>
))}
</ul>
<input
type="text"
placeholder="Ajouter un element"
value={this.state.newItem}
onChange={this.handleChange}
/>
<button onClick={this.addNewItemToState}>Ajouter</button>
</div>
);
}
}
export default StatefullComponent;
Playground

How to mark dynamically created option as used in options list

I want to render options in select dynamically from the list.
If option already used I want to mark it as isUsed: true.
I mean change state of <Wrapper />
this.state = {
options: {
One: {value: "one", isUsed: false},
Two: {value: "two", isUsed: false},
Three: {value: "three", isUsed: false}
}
What is the best way?
I'm trying to mark it using componentDidMount() using markUsed() (for testing purpose there is static key "One"), but how do I get the current mounted option, to mark dynamic key in this.state?
I've tried to console.log(this) in componentDidMount(), but it seems like it doesn't contain current mounted option value.
import React from 'react';
import ReactDOM from 'react-dom';
class Input extends React.Component {
componentDidMount = () => {
console.log(this);
this.props.markUsed();
}
render() {
let options = Object.keys(this.props.list).map((item,i) => {
if (!this.props.list[item].isUsed) {
return(
<option key={i} value={this.props.list[item].value}>{item}</option>
)
}
});
return (
<div>
<select>
{options}
</select>
<input type="text" />
</div>
)
}
}
class Wrapper extends React.Component {
constructor(props) {
super(props);
this.markUsed = this.markUsed.bind(this);
this.state = {
options: {
One: {value: "one", isUsed: false},
Two: {value: "two", isUsed: false},
Three: {value: "three", isUsed: false}
},
inputs: []
}
}
markUsed = () => {
this.setState(prevState =>({
options: {
...prevState.options,
One: {
...prevState.options.One,
isUsed: true
}
}
}));
}
addInput = (e) => {
this.setState((prevState) => ({
inputs: [...prevState.inputs, {option: "", value: ""}],
}));
}
render() {
return(
<div>
{
this.state.inputs.map((val, idx) => {
return (
<div key={idx}>
<Input list={this.state.options} markUsed={this.markUsed} />
</div>
)
})
}
<button type="button" onClick={this.addInput}>add</button>
</div>
)
}
}
ReactDOM.render(<Wrapper />, document.getElementById('root'));
Here is a generic solution where you pass a target to be marked:
markUsed = target => {
const [key, keyValue] = Object.entries(this.state.options).find(
([, keyValue]) => keyValue.value === target
);
keyValue.isUsed = true;
this.setState(prevState => ({
...prevState,
options: { ...prevState.options, [key]: keyValue }
}));
};
Therefore you need to pass a target value like one,two,three etc.
class Input extends React.Component {
state = {
...
};
render() {
const { markUsed } = this.props;
const options = Object.keys(this.props.list).map((item, i) => {
if (!this.props.list[item].isUsed) {
if (this.props.list[item].value === this.state.currValue) {
markUsed(this.state.currValue);
}
return ...
}
});
return (
<>
<select
value={this.state.value}
onChange={e => {
e.persist();
const currValue = e.target.value;
markUsed(currValue);
this.setState({ currValue });
}}
>
...
</>
);
}
}

Dynamic links firebase UI

I want to render a list of restaurants inside select tag with options so that the user can choose a restaurant.
What i try so far:
class Banner extends Component {
this.state = {
restaurant: "",
}
restaurantlist = () => {
var res=[]
var { restaurants } = this.props;
restaurants = restaurants.filter(rest => {
return rest.name.toUpperCase().indexOf(this.state.restaurant.toUpperCase()) !== -1 &&
rest.publish === true })
console.log(restaurants)
res = restaurants.map((item) => (
<li key={item.id}>{item.name}</li>))
return res;
}
<input onChange={(e)=>{ const restaurant = e.target.value;
this.setState({restaurant:restaurant})}}
type="text" name="restaurant" className="res__search"
placeholder="Restaurant"
/>
First you need some corrections with state:
this.state = {
restaurants: [],
// ...
}
You need restaurants as Array not as String.
This is a basic implementation of Select/Option with React:
class Test extends React.Component {
constructor(props) {
super(props);
this.state = {
restaurants: [
{ id: 1, name: 'First' },
{ id: 2, name: 'Second' },
{ id: 3, name: 'Third' }
],
selected: 2,
};
}
handleChange = e => {
this.setState({
selected: e.target.value,
})
}
render() {
return (
<div>
<select value={this.state.selected} onChange={this.handleChange}>
{this.state.restaurants.map(x => <option value={x.id}>{x.name}</option>)}
</select>
<h4>State:</h4>
<pre>{JSON.stringify(this.state, null, 2)}</pre>
</div>
);
};
}
React.render(<Test />, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.9/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/0.14.9/react-dom.min.js"></script>
<div id="app"></div>
Note:
Please read React documentation and also take a look at this example

React JS: How to make this delete button work

I have a simple ToDo application written in React. I cannot figure out how to remove the ToDos.
I have two separate files App.js
import React, { Component } from 'react';
import './App.css';
import ToDo from './components/ToDo.js';
class App extends Component {
constructor(props) {
super(props);
this.state = {
todos: [
{ id: 1, description: 'Walk the cat', isCompleted: true },
{ id: 2, description: 'Throw the dishes away', isCompleted: false },
{ id: 3, description: 'Buy new dishes', isCompleted: false}
],
newTodoDescription: ''
};
this.deleteTodo = this.deleteTodo.bind(this);
}
handleChange(e) {
this.setState({ newTodoDescription: e.target.value })
}
handleSubmit(e) {
e.preventDefault();
if (!this.state.newTodoDescription) { return }
const newTodo = { description: this.state.newTodoDescription, isCompleted: false };
this.setState({ todos: [...this.state.todos, newTodo], newTodoDescription: '' });
}
toggleComplete(index) {
const todos = this.state.todos.slice();
const todo = todos[index];
todo.isCompleted = todo.isCompleted ? false : true;
this.setState({ todos: todos });
}
deleteTodo(id) {
this.setState((prevState) => ({
items: prevState.items.filter(item => item.id !== id),
}))
}
render() {
return (
<div className="App">
<form onSubmit={ (e) => this.handleSubmit(e)}>
<input type="text"
value={ this.state.newTodoDescription }
onChange={ (e) => this.handleChange(e) }
/>
<input type="submit" />
</form>
<ul>
{ this.state.todos.map( (todo, index) =>
<ToDo key={ index }
description={ todo.description }
isCompleted={ todo.isCompleted }
toggleComplete={ () => this.toggleComplete(index) }
onDelete={ this.deleteTodo }
/>
)}
</ul>
</div>
);
}
}
export default App;
The second file is ToDo.js
import React, { Component } from 'react';
class ToDo extends Component {
render() {
return (
<li>
<input type="checkbox" checked={ this.props.isCompleted } onChange={ this.props.toggleComplete } />
<button onClick={() => this.props.deleteTodo(this.props.id)}>Delete</button>
<span>{ this.props.description }</span>
</li>
);
}
}
export default ToDo;
When I click on the button I am presented with an error: TypeError: _this2.props.deleteTodo is not a function
How can I get my current code to work?
The name of your prop is onDelete,
so in your component ToDo.js, you have to call your function like below
<button onClick={() => this.props.onDelete(this.props.id)}>Delete</button>
as answer to your comment
your state is define like
this.state = {
todos: [
{ id: 1, description: 'Walk the cat', isCompleted: true },
{ id: 2, description: 'Throw the dishes away', isCompleted: false },
{ id: 3, description: 'Buy new dishes', isCompleted: false}
],
newTodoDescription: ''
};
So, your function deleteTodo should be like
deleteTodo(id) {
this.setState((prevState) => ({
todos: prevState.todos.filter(item => item.id !== id),
}))
};
I'll suggest you should use the splice method.

Resources