简体   繁体   English

如何在单击按钮时获取函数计算的值

[英]How to get value which function calculates while clicking on button

I am clicking on a button, what It should do is trigger a function and add a new property inside object which is inside a large array of multiple objects, and eventually returns a new array, All I want to know is how we can use that new Array returned by that function?我点击一个按钮,它应该做的是触发一个函数并在对象内部添加一个新属性,该对象位于多个对象的大数组中,并最终返回一个新数组,我只想知道我们如何使用它该函数返回的新数组?

I am using this function to get triggered to change the array which I am passing it it, but I am not sure how I can get this value back.我正在使用这个函数来触发更改我传递给它的数组,但我不确定如何取回这个值。 Sorry in advance for stupid question I am new to this.提前抱歉愚蠢的问题我是新来的。

const makeAdminPresenter = (collection) => {
        var newTiles = collection?.map( obj => {
            if(obj.userType === "administrator") {
                return {...obj, isAdminPresenting: true}
            }
            return obj;
        });
        console.log('pressed', newTiles)
        return newTiles

    };
    
    var newTiles = useCallback(() => {
         makeAdminPresenter(tiles);
    },[makeAdminPresenter, tiles]) 

    console.log('result',makeAdminPresenter,newTiles  )


I want to use newTiles, any suggestion?我想使用 newTiles,有什么建议吗?

in your code you should make use of states (using useState when having functional components) to store the state of the tiles.在您的代码中,您应该使用状态(在具有功能组件时使用useState )来存储图块的状态。 The makeAdminPresenter modifies the tiles and you will have to re-render the component after the tiles are updated. makeAdminPresenter会修改图块,您必须在图块更新后重新呈现组件。 The code snippet below has an example on how this could be done:下面的代码片段有一个关于如何做到这一点的例子:

export default function Page() {
    const [tiles, setTiles] = React.useState([...]); // The tiles, you probably initialise elsewhere

    const makeAdminPresenter = () => {
        var newTiles = tiles.map(obj => {
            if (obj.userType === "administrator") {
                return {...obj, isAdminPresenting: true}
            }
            return obj;
        });
        console.log('pressed', newTiles)

        setTiles(newTiles); // Update the state so that the component gets re-rendered
    };

    console.log('result', makeAdminPresenter, tiles) // After the button is pressed tiles will be the new tiles
    return (<>
            <button onClick={makeAdminPresenter}></button>
            {tiles.map((tile, i) => (<TileComponent key={i} tile={tile}/>))}
        </>
    )
}

Hope this helps!希望这可以帮助!

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

相关问题 如何将“日期”类型的输入值传递给每秒计算和更新年龄的 function - How to pass input value of type 'date' to a function which Calculates and updates age every second 如何通过单击其按钮来在td中获取价值 - How to get value in a td by clicking its button 如何通过单击按钮获取单元格值 - How to get cell value by clicking on a button 如何通过单击按钮获取更改时的选项值? - How to get the option value on change by clicking on the button? 需要一个JavaScript函数来识别单击“保存”按钮时使用的文本框 - Need a javascript function to identify the which textbox used while clicking save button 如何在单击按钮时更改onclick函数值 - How to change the onclick function value upon clicking a button 如何通过单击按钮获取 div html 中的文本值 - How to get the text value in a div html by clicking on button 单击 Slack 应用程序上的按钮时如何获取字段值? - How to get the field value when clicking on a button on a Slack app? 如何通过单击使用循环动态创建的按钮来获取按钮的值 - how to get the value of the button by clicking that was created dynamically using loop 如何通过单击DataTable的同一行上的“编辑”按钮获取Id值 - How to get Id value by clicking Edit button on the same row of DataTable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM