简体   繁体   English

如何将钩子导出到 react.js 中的其他函数?

[英]How to export hooks to other functions in react.js?

i am trying to export a hook {count, setCount} = useState(0) to another function to be able to get {count} value.我正在尝试将钩子 {count, setCount} = useState(0) 导出到另一个函数,以便能够获得 {count} 值。 My app is to make a simple inventory by adding items then when press submit button get a render of this inventory.我的应用程序是通过添加项目来制作一个简单的清单,然后在按下提交按钮时获取此清单的渲染。

 import React, { useState } from "react"; import objects from "../list"; const List = function _List(item) { const [count, setCount] = useState(0); const increase = () => { return count < 10 ? setCount(count + 1) : 10; }; const decrease = () => { return count > 0 ? setCount(count - 1) : 0; }; return ( <div className="flex"> <h3>{item.name}</h3> <button type="submit" onClick={decrease}> - </button> <h3>{count}</h3> <button type="submit" onClick={increase}> + </button> </div> );}; function App() { const [message, setMessage] = useState("No Order Submited"); function displayOrder() { const orderList = setMessage(objects.map((x) => `${x.name} `)); // Would like to export {count} in const orderList return { orderList }; } return ( <div> <fieldset> <h1>Items:</h1> {objects.map(List)} </fieldset> <button type="submit" onClick={displayOrder}> Submit </button> <h2>{message}</h2> </div> ); } export default App;

Here is the objects list of my inventory:这是我的库存的对象列表:

 const objects = [ { id: 1, name: "item1", }, { id: 2, name: "item2", } ]; export default objects;

The best way would be to move my hook into the App function but the thing is increasing or decreasing {count} would affect all my items.最好的方法是将我的钩子移到 App 函数中,但增加或减少 {count} 会影响我的所有项目。

Is there anyway to render the {count} for each items when running the function displayOrder?无论如何,在运行函数 displayOrder 时是否要为每个项目呈现 {count}?

Thanks!谢谢!

The main way you'd probably want to do it is by moving the state to the parent component.您可能想要这样做的主要方法是将状态移动到父组件。 So something like this:所以像这样:

 const {useState} = React; const exampleObjects = [ {id: 1, name: "item1"}, {id: 2, name: "item2"} ]; // It makes react devs happy when you avoid mutating state. const update = (arr, i, newVal) => [ ...arr.slice(0,i), newVal, ...arr.slice(i+1) ]; function List({name, count, setCount}) { const increase = () => { return count < 10 ? setCount(count + 1) : 10; }; const decrease = () => { return count > 0 ? setCount(count - 1) : 0; }; return ( <div className="flex"> <h3>{name}</h3> <button type="submit" onClick={decrease}> - </button> <h3>{count}</h3> <button type="submit" onClick={increase}> + </button> </div> ); } function App({objects}) { // I would actually recommend you make counts an object, but I figured I'd leave the .reduce fun as an exercise for you. // Note this also gets rid of the update function silliness, allowing you to do something like {...oldObj, [id]: newVal} instead. const [counts, setCounts] = useState(objects.map(_ => 0)); const [order, setOrder] = useState(null); const setCount = (i) => (newVal) => { setCounts(cs => update(cs,i,newVal)); }; const submitOrder = () => { setOrder(objects.map((o,i) => ({...o, count: counts[i]}))); }; return ( <div> <fieldset> <h1>Items:</h1> {objects.map((o, i) => (<List key={o.id} {...o} count={counts[i]} setCount={setCount(i)} />))} </fieldset> <button type="submit" onClick={submitOrder}> Submit </button> <h2>Orders</h2> {!order ? "No order submitted" : order.map(({id, name, count}) => ( <div key={id}> {name}: {count} </div> ))} </div> ); } // Render it ReactDOM.render( <App objects={exampleObjects} />, document.getElementById("react") );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="react"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM