[英]re-render component in react to get default props again
I'm using package react-calendar I'm gonna customize it to select just month, for example 2020-02 or 2020-05 , ... and also It should be hidden first and displayed when I click the button (Icon).我正在使用包react-calendar我将对其进行自定义以仅选择月份,例如 2020-02 或 2020-05 ,......而且它应该首先隐藏并在我单击按钮(图标)时显示。 so I wrote a component to toggle it, named Dropdown , plus I set view prop of calendar to show months of year when it appears and when I click on a month, the hide state changes and Dropdown will close and a month would be selected.
所以我写了一个组件来切换它,命名为 Dropdown ,另外我设置了日历的视图属性以在它出现时显示一年中的月份,当我点击一个月份时,隐藏状态发生变化并且 Dropdown 将关闭并选择一个月。
However the problem is when I wanna click again on the button select another month , the calendar starts from days not month view because after selecting month it goes to select day.然而问题是当我想再次点击按钮选择另一个月份时,日历从天而不是月视图开始,因为选择月份后它会选择日期。 actually it needs to re-render or set again 'year' to it's view props.
实际上它需要重新渲染或再次设置“年份”到它的视图道具。
Is there any way to re-render Calendar component when state changes.有没有办法在状态改变时重新渲染 Calendar 组件。
import section:导入部分:
import ReactCalendar from "react-calendar";
state section:状态部分:
const [hide, sethide] = useState(false);
return section:退货部分:
<Dropdown
forceHide={hide}
onChangeShow={() => sethide(false)}
toggle={
<Icon name="calendar" size={32} />
}
>
<ReactCalendar
view={'year'}
onClickMonth={value => {
console.log("value", value);
sethide(true);
}}
/>
</Dropdown>
any helps is appreciated.任何帮助表示赞赏。
I solve my problem in this way but I'm not sure it's an optimize solution if you have any idea please share with me.我以这种方式解决了我的问题,但我不确定这是一个优化的解决方案,如果您有任何想法,请与我分享。
I defined a function to clone it in each render occurs:我定义了一个函数来在每次渲染时克隆它:
const RenderReactCalendar = ({view})=>{
let Render = (
<ReactCalendar
onClickMonth={value => {
console.log("value", value);
sethide(true);
}} />
);
return React.cloneElement(Render, { view }, {});
};
and then refactor my component:然后重构我的组件:
<Dropdown
forceHide={hide}
onChangeShow={() => sethide(false)}
toggle={
<Icon name="calendar" size={32} />
}
>
<RenderReactCalendar
view={'year'}
/>
</Dropdown>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.