【发布时间】:2023-02-15 07:04:25
【问题描述】:
在下面的代码中,每当我从父级获得新道具时,新道具都会正确记录在控制台上,但渲染的 HTML 在初始渲染后永远不会更新:
export default function(props) {
const [state, setState] = useState(props)
// initially, props.something is defined
// every time props changes (from the parent) props.something is redefined as expected and logged here
console.log(props.something)
// initially, props.something is rendered correctly
// every time props.something changes (from the parent) the HTML never updates
return (
{state.something && <div>{state.something}</div>}
)
}
我已经尝试使用useEffect(),尽管我看不出有什么意义,但它并没有解决任何问题。
【问题讨论】:
标签: reactjs