如何在 React 中动态设置 defaultProps
How to dynamically set defaultProps in React
一直在尝试找到一种在 React 中为道具动态设置默认道具的方法
Shop.defaultProps = {
address: {props.address}
status: 'open'
};
有没有办法动态设置默认道具?
React 道具是不可变的,如果你想改变某些东西,那么它需要处于状态。默认 props 更像是一个“警卫”来防止 null/undefined 值,或者如果你真的想要一个默认值 value/setting.
如果道具是动态的,则不需要“设置”,而是稍后在组件中通过状态对其进行处理。
一直在尝试找到一种在 React 中为道具动态设置默认道具的方法
Shop.defaultProps = {
address: {props.address}
status: 'open'
};
有没有办法动态设置默认道具?
React 道具是不可变的,如果你想改变某些东西,那么它需要处于状态。默认 props 更像是一个“警卫”来防止 null/undefined 值,或者如果你真的想要一个默认值 value/setting.
如果道具是动态的,则不需要“设置”,而是稍后在组件中通过状态对其进行处理。