MCPcopy Index your code
hub / github.com/react/react / commitMount

Function commitMount

packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js:808–867  ·  view source on GitHub ↗
(
  domElement: Instance,
  type: string,
  newProps: Props,
  internalInstanceHandle: Object,
)

Source from the content-addressed store, hash-verified

806export const supportsMutation = true;
807
808export function commitMount(
809 domElement: Instance,
810 type: string,
811 newProps: Props,
812 internalInstanceHandle: Object,
813): void {
814 // Despite the naming that might imply otherwise, this method only
815 // fires if there is an `Update` effect scheduled during mounting.
816 // This happens if `finalizeInitialChildren` returns `true` (which it
817 // does to implement the `autoFocus` attribute on the client). But
818 // there are also other cases when this might happen (such as patching
819 // up text content during hydration mismatch). So we'll check this again.
820 switch (type) {
821 case 'button':
822 case 'input':
823 case 'select':
824 case 'textarea':
825 if (newProps.autoFocus) {
826 ((domElement: any):
827 | HTMLButtonElement
828 | HTMLInputElement
829 | HTMLSelectElement
830 | HTMLTextAreaElement).focus();
831 }
832 return;
833 case 'img': {
834 // The technique here is to assign the src or srcSet property to cause the browser
835 // to issue a new load event. If it hasn't loaded yet it'll fire whenever the load actually completes.
836 // If it has already loaded we missed it so the second load will still be the first one that executes
837 // any associated onLoad props.
838 // Even if we have srcSet we prefer to reassign src. The reason is that Firefox does not trigger a new
839 // load event when only srcSet is assigned. Chrome will trigger a load event if either is assigned so we
840 // only need to assign one. And Safari just never triggers a new load event which means this technique
841 // is already a noop regardless of which properties are assigned. We should revisit if browsers update
842 // this heuristic in the future.
843 if (newProps.src) {
844 const src = (newProps: any).src;
845 if (enableSrcObject && typeof src === 'object') {
846 // For object src, we can't just set the src again to the same blob URL because it might have
847 // already revoked if it loaded before this. However, we can create a new blob URL and set that.
848 // This is relatively cheap since the blob is already in memory but this might cause some
849 // duplicated work.
850 // TODO: We could maybe detect if load hasn't fired yet and if so reuse the URL.
851 try {
852 setSrcObject(domElement, type, src);
853 return;
854 } catch (x) {
855 // If URL.createObjectURL() errors, it was probably some other object type
856 // that should be toString:ed instead, so we just fall-through to the normal
857 // path.
858 }
859 }
860 ((domElement: any): HTMLImageElement).src = src;
861 } else if (newProps.srcSet) {
862 ((domElement: any): HTMLImageElement).srcset = (newProps: any).srcSet;
863 }
864 return;
865 }

Callers

nothing calls this directly

Calls 2

setSrcObjectFunction · 0.90
focusMethod · 0.65

Tested by

no test coverage detected