简体   繁体   English

更改 state 时不调用 Function

[英]Function is not called when state is changed

I have a function in react showPeople.我在 react showPeople 中有一个 function。 This is the code:这是代码:

const showPeople = () => {
    const people = workers.map((worker, i) => {
        return (
            <div>
                <Worker
                    id={worker.id}
                    index={i}
                    role={worker.role}
                />
            </div>
        )
    })
    return people;
}

Later in the component I want to call this function in order to show me some information for the workers.稍后在组件中,我想将其称为 function,以便向我展示一些工人的信息。 I call it like this:我这样称呼它:

<div>
  {showPeople()}
</div> 

And this is ok, but it is called only once.这没关系,但它只被调用一次。 When I change the state it's not called.当我更改 state 时,它不会被调用。 So I have my updates only after I refresh the page.所以我只有在刷新页面后才有更新。 If i remove the parenthesis in the call, I get nothing on my screen.如果我删除通话中的括号,我的屏幕上什么也没有。 Does anyone knows what's the problem?有谁知道是什么问题? Sorry if this question is a duplicate, I am not sure I understood any of the previous answers connected tot his topic.抱歉,如果这个问题是重复的,我不确定我是否理解与他的主题相关的任何先前的答案。 Thanks谢谢

As this is written, it should update when state updates ( see example here ).正如本文所写,它应该在state更新时更新(参见此处的示例)。 Unless you have the array workers saved for example like const {workers} = this.state or const [workers, setWorkers] = useState([{'id': '1', role: 'thisRoleNAme'}]) then you may not actually be accessing a piece of state.除非您保存了数组workers ,例如const {workers} = this.stateconst [workers, setWorkers] = useState([{'id': '1', role: 'thisRoleNAme'}])那么你可能不会实际上正在访问一块 state。

Edit your function to accept workers as parameter:编辑您的 function 以接受工人作为参数:

const showPeople = (workers) => {
 const people = workers.map((worker, i) => {
    return (
        <div>
            <Worker
                id={worker.id}
                index={i}
                role={worker.role}
            />
        </div>
    )
})
return people;
}

And pass workers from state.并从 state 传递工人。

<div>
 {showPeople(this.state.workers)}
</div> 

It should re-render whenever your this.state.workers change and it will render with new workers.每当您的 this.state.workers 更改时,它都应该重新渲染,并且它将与新的工人一起渲染。

暂无
暂无

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

相关问题 当更改其他状态时,将调用render函数中的状态更改事件 - State change event inside render function is getting called when a different state is changed 在更改 state 之前调用我的 function - My function is being called before the state is changed function 内部的 clearInterval 未清除,当调用 function 并反应 state 在执行其他功能后更改 - clearInterval inside of a function is not clearing ,when the function is called and react state changed after executing other functions 如何在 redux state 改变时触发 function? - How to trigger a function when the redux state is changed? 在内部设置状态时循环调用的函数 - Function called on loop when setting state inside of it 为什么使用选中的属性更改复选框时未调用onchange函数 - Why onchange function not called when checkbox changed using checked property React-成员函数在初始状态下(外部构造函数)调用时不是函数 - React - member function is not a function when called in initial state (outside constructor) 当仅调用一个数组进行更改/排序时,React state arrays 正在更改/排序在一起 - React state arrays are changing/being sorted together when only one array is called to be changed/sorted React Function在被称为Twice时更新状态 - React Function updates the state just when it's called Twice 状态是<empty string>在按键事件上调用函数时 - State is <empty string> when function is called on key event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM