Как я могу прикрепить ссылку на компонент без состояния в React?

При использовании функции DrawerLayout в этом случае должно быть только одно основное содержимое View с ящиком View - в этом случае ваш ListView - после него. Использование DrawerLayout любым другим способом приведет к неправильному, непредсказуемому поведению, часто препятствуя нормальному взаимодействию с другими элементами макета.

Учебник со ссылками на образец и документы можно найти на this страница разработчика .

39
задан bx2 28 June 2019 в 22:03
поделиться

1 ответ

Можно также получить судей в функциональные компоненты с небольшой инфраструктурой

import React, { useEffect, useRef } from 'react';

// Main functional, complex component
const Canvas = (props) => {
  const canvasRef = useRef(null);

    // Canvas State
  const [canvasState, setCanvasState] = useState({
      stage: null,
      layer: null,
      context: null,
      canvas: null,
      image: null
  });

  useEffect(() => {
    canvasRef.current = canvasState;
    props.getRef(canvasRef);
  }, [canvasState]);


  // Initialize canvas
  useEffect(() => {
    setupCanvas();
  }, []);

  // ... I'm using this for a Konva canvas with external controls ...

  return (<div>...</div>);
}

// Toolbar which can do things to the canvas
const Toolbar = (props) => {
  console.log("Toolbar", props.canvasRef)

  // ...
}

// Parent which collects the ref from Canvas and passes to Toolbar
const CanvasView = (props) => {
  const canvasRef = useRef(null);

  return (
    <Toolbar canvasRef={canvasRef} />
    <Canvas getRef={ ref => canvasRef.current = ref.current } />
}
1
ответ дан 26 November 2019 в 23:24
поделиться
Другие вопросы по тегам:

Похожие вопросы: