建议:使用 useImperativeHandle hook 代替 ref callback
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- react, typescript
- 領域
- frontend
調査の方向性
既存の Notification.newInstance エントリポイントと、issue で説明されている class-component ref callback から始めます。提案されている forwardRef/useImperativeHandle フローを比較し、add が同期的にキーを返し、remove がそのキーを受け入れ、destroy が引き続きコンテナをアンマウントすることを確認してください。
索引モデルが issue の本文から書いたものです。
説明
背景
为了能在普通 JavaScript 函数中调用 <Notification ref={ref} /> 组件实例的方法,目前是通过 class component 的 ref callback 机制实现的,但因为依赖异步回调,导致上层 API 的使用也深受回调影响,如果可以通过某种方式同步获得组件的控制权,那么在 API 使用和封装上会变得优雅很多:
const instance = Notification.newInstance();
const key = instance.add({title: 'xxx'}); // 可以同步返回 key
instance.remove(key);
我们可以通过 useImperativeHandle hook 结合 ref 转发来实现此需求。
大致思路:
- 使用 Function Component 改写 Notification 组件
- Notification 组件通过 useImperativeHandle hook 和 forwardRef 暴露内部 add 和 remove 方法
- Notification.newInstance 通过 createRef 并把 ref.current 同步返回,这样就可以实现同步调用了
const Notification = React.forwardRef((props, ref) => {
const [notices, setNotices] = useState([]);
const add = notice => {
// add notice
};
const remove = key => {
// remove notice
};
// expose add & remove methods
useImperativeHandle(ref, () => ({
add,
remove,
}));
return (
<div>{noticeNodes}</div>
);
});
Notification.newInstance = function newNotificationInstance(properties) {
const ref = React.createRef();
// append container stuff....
ReactDOM.render(<ToastHub {...props} ref={ref} />, div);
return {
notice: noticeProps => ref.current.add(noticeProps),
removeNotice: key => ref.current.remove(key),
component: ref.current,
destroy() {
ReactDOM.unmountComponentAtNode(div);
div.parentNode.removeChild(div);
},
};
}
- 主要言語
- TypeScript
- スター
- 384
- フォーク
- 118
- 平均マージ
- 1時間 18分
- マージ済み PR(30日)
- 2
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
react-component/notification のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 45/100
react-component/notification#382 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
react-component/notification#356 · コメント 2 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 42/100
react-component/notification#333 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
react-component/notification#307 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 48/100
react-component/notification#305 ·
react-component/notification の issue をすべて見る
似ている issue
-
VerificationGate: ATTRIBUTION quote guard never matches a normal quotation (\b around the quote) オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
danielmiessler/LifeOS#2234 ·
-
T: Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
-
Mend: dependency security vulnerability untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100