#StackBounty: #arrays #reactjs #react-hooks #state #prop How to use a useState function in another component with props?

Bounty: 50

I write a React.js note web application where a user can add up to 10 notes.

I use map() to iterate the array of notes, and a useState(1) hook to update its count (the default number of notes is 1), so I would like to do something like this:

  {[...Array(noteCount)].map((_, i) => <Note onUpdateNoteCount={() =>setNoteCount(n => n - 1)} key={i} />)}

The thing is that the Note() component is inside a Main() component which is in the App() component, so I want to get the needed values as props of App(), and than use them in Note(), but can not figure out how and where to put it.



  import React from 'react';
  import Header from './Header';
  import Main from './Main';
function App () {
  const [noteCount, setNoteCount] = React.useState(1);
  function multiplyNoteComponent () {
    if (noteCount < 20) {
      setNoteCount(n => n + 1)
    else {
      alert('too many notes. remove or combine some of them together!')
  return (
      {[...Array(noteCount)].map((_, i) => <Main onUpdateNoteCount={() =>setNoteCount(n => n - 1)} key={i} />)}

            style={{left: '5%'}}
          title='Add a note'
  export default App;


    import React from 'react';
    import Note from './Note';
function Main () {
    return (
            your notes are:
    export default Main;


    import React from 'react';
function Note () {
    return (
        <div> <button title='delete note' onClick={}>X</delete>
                <input type='text'/>
    export default Note

Edit: the reason I think I need the setNoteCount() function to be used in the Note() component, is for the count down when a note is being deleted (every note has its own delete button).

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.