JavaScript How do I pass a value from an array defined in state into a component which beholds a component? pass value from child to parent react,pass value from

So I am new to React and trying to write a Todo list.

As you will see in the following snippets I define an array of sample todos in the App.js. I call the TodoList component in the App.js and hand it the array. In the TodoList I would like to generate for the amout of elements in my list a single Todo with a dynamic value the title. I do get the title on the page but through the Todolist. How can I hand that value to the single Todo, and does that even makes sense?

App.js

class App extends Component {

  state = {
    todos: [
      {
        id: uuid.v4(),
        title: 'Title',
        isCompleted: false
      },
      {
        id: uuid.v4(),
        title: 'Title 2',
        isCompleted: false
      },
      {
        id: uuid.v4(),
        title: 'Title 3',
        isCompleted: false
      }
    ]
  }

    render() {
        return (
      <div className="App">
        <LayoutEntry />
        <AddTodo />
        <TodoList todos={this.state.todos} />
      </div>
     );
    }
}
export default App;

TodoList.js

class TodoList extends Component {
    render() {
        return this.props.todos.map((todo) => (
          <div>
            <h3>{ todo.title += " Test" }</h3> 
            <Todo />
          </div>
        ));
    }
}
export default TodoList;

Todo.js

class Todo extends Component {
    render() {
        return (
      <div>
        <input id="buttonDelete" type="button" value="Delete"></input>
      </div>   
      )
    }
}
export default Todo;
Answer:1

Just pass it as a prop in your map:

<Todo item={todo} key={todo.id} />

Then in your Todo component, you can grab the data:

<div>
    <div>Title: {this.props.item.title}</div>
</div>

EDIT: Added the key as pointed out by Richard Fazzi.

Answer:2

Well, I'm learning react too and I have this to say to you:

Every list must have a key, like this:

const menu = this.props.dishes.map((dish) => {
            return (
                <div key={dish.id} className="col-12 mt-5">
                    <Media tag="li">
                        <Media left middle>
                            <Media object src={dish.image} alt={dish.name}/>
                        </Media>
                        <Media body className="ml-5">
                            <Media heading>{dish.name}</Media>
                            <p>{dish.description}</p>
                        </Media>
                    </Media>
                </div>
            );
        });

And I really think you should use a list.

The other thing I think you should do is to create a variable with the returned value from the map, then you can return that, eaxmple:

return (
            <div className="container">
                <div className="row">
                    <Media list>
                        { menu }
                    </Media>
                </div>
            </div>
        );

Hope it helps

Answer:3

I'm trying to "insert" an alerter if the username of a basic form is not filled and I have been helplessly trying to understand why the anonymous function within a submit function doesn't get called. ...

I'm trying to "insert" an alerter if the username of a basic form is not filled and I have been helplessly trying to understand why the anonymous function within a submit function doesn't get called. ...

  1. calling anonymous function matlab
  2. calling anonymous function javascript
  3. calling anonymous function scala
  4. calling anonymous function jquery
  5. self calling anonymous function javascript
  6. javascript calling anonymous function recursively
  7. java calling anonymous function
  8. anonymous function calling itself

I have a table and using ng-repeat data is populated, which is obtained from JSON Table has name, phone and time when i click on name a bootstrap modal pops up, but i need to have selected name ...

I have a table and using ng-repeat data is populated, which is obtained from JSON Table has name, phone and time when i click on name a bootstrap modal pops up, but i need to have selected name ...

  1. bootstrap modal should not close on escape
  2. bootstrap modal should not close on click
  3. bootstrap modal should not close when click outside

When is asObservable() needed on a Subject (e.g. BehaviorSubject) to get a observable of the subject? The subject isself can be casted to an Observable as well. Questions What are the technical ...

When is asObservable() needed on a Subject (e.g. BehaviorSubject) to get a observable of the subject? The subject isself can be casted to an Observable as well. Questions What are the technical ...

I wrote a gulp file that works but...not at any attempts. Indeed, I get this weird error 1 time / 3: [12:25:51] Starting 'default'... [12:25:51] Starting 'clean'... [12:25:51] Finished 'clean' ...

I wrote a gulp file that works but...not at any attempts. Indeed, I get this weird error 1 time / 3: [12:25:51] Starting 'default'... [12:25:51] Starting 'clean'... [12:25:51] Finished 'clean' ...

  1. gulp unhandled stream error in pipe
  2. gulp uglify unhandled stream error in pipe