Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Best (functional) way in React to lift state an arbitrary number of levels?

I saw this post from 2017 where it was asked how to lift state up two levels in react. The answer used this in the context of an OOP design. Five years later, components can be written in functional programming, largely omitting usage of the this keyword.

What’s the simplest way to lift state up any arbitrary number, n, levels? Is this something best accomplished with tools like redux or is vanilla React sufficient?

For example, I’m unsure how to best pass deleteItemHandler (noted in inline comments) from App to Item, passing through Display.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function App() {
  const [todoList, todoListSetter] = useState([]);

  const addTodoHandler = (item) => {
    todoListSetter((prevItems) => {
      return [item, ...prevItems]
    });
  };

  const deleteItemHandler = (item) => { //Level 1
    todoListSetter((prevItems) => {
      return prevItems.filter(elem => elem !== item)
    });
  };
  
  return (
    <div >
      <Display removeItem={deleteItemHandler} items={todoList} /> //Level 1->2      
      <Form onSaveItem={addTodoHandler}/>
    </div>
  );
};

const Display = (props) => {
  props.items.map((item) => {
    return(
    <div>
      <Item deleteItemHandler={props.removeItem} value={item}/> //Level 2->3
    </div>)
  });
};

const Form = (props) => {
    
    const [newItem, setNewItem] = useState("");
    const newItemHandler = (event) => {
      setNewItem(event.target.value);
    };   
    
    const submitHandler = (event) => {
        event.preventDefault();
        props.onSaveItem;

    };
    
    return(
        <div>
            <h2>Add item to todo list</h2>
            <form onSubmit={submitHandler}>
                <label>New Item</label>
                <input type='text' onChange={newItemHandler} />
            </form>
        </div>
    ) 
};

const Item = (props) => {
    
    return(
        <div>
            <h1>{props.value}</h1>
            <button onClick={props.deleteItemHandler}>Delete Item</button> //Level 3
        </div>
    )
};

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

>Solution :

Here is the example given from the React documentation for the useContext hook:

const themes = {
  light: {
    foreground: "#000000",
    background: "#eeeeee"
  },
  dark: {
    foreground: "#ffffff",
    background: "#222222"
  }
};

const ThemeContext = React.createContext(themes.light);

function App() {
  return (
    <ThemeContext.Provider value={themes.dark}>
      <Toolbar />
    </ThemeContext.Provider>
  );
}

function Toolbar(props) {
  return (
    <div>
      <ThemedButton />
    </div>
  );
}

function ThemedButton() {
  const theme = useContext(ThemeContext);
  return (
    <button style={{ background: theme.background, color: theme.foreground }}>
      I am styled by theme context!
    </button>
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading