我有一个带有三种淡入淡出状态的 ReasonReact 覆盖组件。第一个在完全不透明时可见,第二个在几乎完全透明时可见,第三个是不显示。
isOpen
有没有办法使用生命周期事件来避免轮询以查看是否已切换外部道具?
我已经尝试从 didReceiveProps 和 render 发送更新(但在这两种方法中都出现了无限循环或主要闪烁)。
我当前的组件。
type fading =
| FadedIn
| Intermediate
| FadedOut;
type cycle =
| In(fading)
| Out(fading);
type action =
| Cycle
| PassTheCycle;
type state = {cycling};
let component = React.reducerComponent(__MODULE__);
let make =
(~isOpen, ~onClick=None, children) => {
...component,
didMount: ({send, onUnmount}) => {
let interval = Js.Global.setInterval(() => PassTheCycle->send, 200);
onUnmount(() => Js.Global.clearInterval(interval));
},
reducer: (action: action, state: state) => {
let delay = (~delay=20, next) =>
React.UpdateWithSideEffects(
{cycling: next},
({send}) => Js.Global.setTimeout(() => Cycle->send, delay)->ignore,
);
switch (action) {
| PassTheCycle =>
switch (state.cycling) {
| Out(FadedIn)
| In(FadedOut) => React.SideEffects((({send}) => Cycle->send))
| _ => React.NoUpdate
}
| Cycle =>
switch (state.cycling) {
| In(x) =>
switch (x) {
| FadedIn => React.NoUpdate
| Intermediate => delay(~delay=200, In(FadedIn))
| FadedOut => delay(In(Intermediate))
}
| Out(x) =>
switch (x) {
| FadedOut => React.NoUpdate
| Intermediate => delay(~delay=200, Out(FadedOut))
| FadedIn => delay(Out(Intermediate))
}
}
};
},
initialState: () => {cycling: isOpen ? In(FadedIn) : Out(FadedOut)},
willReceiveProps: ({state}) =>
switch (isOpen, state.cycling) {
| (false, In(x)) => {cycling: Out(x)}
| (true, Out(x)) => {cycling: In(x)}
| _ => state
},
render: ({state}) => {
let className =
Style.main(state.cycling);
<div className> ...children </div>;
},
};