【发布时间】:2020-04-02 09:30:24
【问题描述】:
我正在尝试使子功能组件在父组件更改其状态中的值时更新,我将其作为道具传递给此子组件。
子组件正确“接收”值并显示道具值,但该方法不再运行。
子组件
import React from 'react'
const MyCustomTable = props => {
const {
data = [],
} = props
const finalData = getSalesData() //This is the method i want to run when the selectedMonth prop updates
const getSalesData = () => {
//It does some calculations with the prop called data
}
return (
<Box>
{JSON.stringify(props.selectedMonth.value)}
<Table
data={finalData}
/>
</Box>
)
}
SalesByFamilyBU.propTypes = {}
export default MyCustomTable
JSON.stringify 行正确显示更改,但我猜 getSalesData() 不会自动执行。
【问题讨论】:
-
你怎么知道这个方法不会触发?尝试在
getSalesData函数中添加console.log() -
如果您在尝试调用函数之前定义了函数会有所帮助。
标签: reactjs