简体   繁体   English

渲染道具组件抛出对象作为 React 子组件无效

[英]Render props component throws Objects are not valid as a React child

I am trying to make my own debounce input element, where I could send whichever input component I need like textarea and input and make it debounce.我正在尝试制作我自己的去抖动输入元素,我可以在其中发送我需要的任何输入组件,例如 textarea 和 input 并使其去抖动。 I have made a debounceComponent that looks like this:我制作了一个看起来像这样的 debounceComponent:

import { useState, useCallback } from "react";
import debounce from "lodash.debounce";

const useDebounce = (callback, delay) => {
  const debouncedFn = useCallback(
    debounce((...args) => callback(...args), delay),
    [delay] // will recreate if delay changes
  );
  return debouncedFn;
};

function DebouncedInput(props) {
  const [value, setValue] = useState(props.initialValue);
  const debouncedSave = useDebounce(
    (nextValue) => props.onChange(nextValue),
    1000
  );

  const handleChange = (event) => {
    const { value: nextValue } = event.target;
    setValue(nextValue);
    debouncedSave(nextValue);
  };

  return props.renderProps({ ...props, handleChange, value });

  //return <textarea value={value} onChange={handleChange} rows={5} cols={50} />;
}

export default DebouncedInput;

And this is how I use it:这就是我使用它的方式:

 <DebouncedInput
    initialValue={value}
    onChange={handleChange}
    rows={5}
    cols={50}
    renderProps={(props) => <TextArea {...props} />}
  />

But, if I use it like that I get an error:但是,如果我像这样使用它,则会出现错误:

Objects are not valid as a React child (found: object with keys {dispatchConfig, _targetInst, _dispatchListeners, _dispatchInstances, nativeEvent, type, target, currentTarget, eventPhase, bubbles, cancelable, timeStamp, defaultPrevented, isTrusted, isDefaultPrevented, isPropagationStopped}).对象作为 React 子对象无效(发现:具有键 {dispatchConfig、_targetInst、_dispatchListeners、_dispatchInstances、nativeEvent、type、target、currentTarget、eventPhase、bubbles、cancelable、timeStamp、defaultPrevented、isTrusted、isDefaultPrevented、isPropagationStopped} 的对象)。 If you meant to render a collection of children, use an array instead.如果您打算渲染一组子项,请改用数组。

You can see the codesandbox for it here .你可以在这里看到它的代码和框。 What am I doing wrong here, how can I fix this?我在这里做错了什么,我该如何解决?

In your DebouncedInput component change the return statement.在您的DebouncedInput组件中更改 return 语句。

from

return props.renderProps({ ...props, handleChange, value });

to

return props.renderProps({ ...props, onChange: handleChange, value });
import { useState, useCallback } from "react";
import debounce from "lodash.debounce";

const useDebounce = (callback, delay) => {
  const debouncedFn = useCallback(
    debounce((...args) => callback(...args), delay),
    [delay] // will recreate if delay changes
  );
  return debouncedFn;
};

function DebouncedInput(props) {
  const [value, setValue] = useState(props.initialValue);
  const debouncedSave = useDebounce(
    (nextValue) => props.onChange(nextValue),
    1000
  );

  const handleChange = (event) => {
    const { value: nextValue } = event.target;
    setValue(nextValue);
    debouncedSave(nextValue);
  };

  return props.renderProps({ ...props, onChange: handleChange, value });

}

export default DebouncedInput;

Also, instead of spreading all the props to the component, pass only the props that are relevant to the input element.此外,不是将所有 props 传播到组件,而是只传递与 input 元素相关的 props。 So, in this case when calling props.renderProps({ ...props, onChange: handleChange, value }) all the props that's being received by DebouncedInput component are directly passed to the input or TextArea component which means renderProps is also being passed.因此,在这种情况下,当调用props.renderProps({ ...props, onChange: handleChange, value })所有这一切由接收的道具DebouncedInput构件直接传递到inputTextArea该装置部件renderProps也被传递。 But in general input or TextArea might not have initialValue , renderProps as props, that will throw a warning.但在一般情况下inputTextArea可能没有initialValuerenderProps作为道具,这会引发警告。

There are different ways of in order not to get such warnings, below is one of the way有多种方法可以避免收到此类警告,以下是其中一种方法

  • Spread the required props to DebouncedInput and the props of input component as rest parameter将所需的 props 传播到DebouncedInput并将输入组件的 props 作为rest参数
function DebouncedInput({initialValue, renderProps, onChange, ...rest}) {
  const [value, setValue] = useState(initialValue);
  const debouncedSave = useDebounce(
    (nextValue) => onChange(nextValue),
    1000
  );

  const handleChange = (event) => {
    const { value: nextValue } = event.target;
    setValue(nextValue);
    debouncedSave(nextValue);
  };

  return renderProps({ ...rest, onChange: handleChange, value });

}
  • Pass all the input/TextArea related props inside another object like below.将所有与 input/TextArea 相关的道具传递到另一个对象中,如下所示。 Here I'm passing all the related props that I want to send as part of input component I'm wrapping inside inputProps and passing the same through renderProps .在这里,我传递了我想作为输入组件的一部分发送的所有相关道具,我将其包装在inputProps并通过inputProps传递相同的renderProps
<DebouncedInput
    initialValue={value}
    onChange={handleChange}
    renderProps={(props) => <TextArea {...props} />}
    inputProps={{rows:5, cols:50}}
  />
function DebouncedInput(props) {
  const [value, setValue] = useState(props.initialValue);
  const debouncedSave = useDebounce(
    (nextValue) => props.onChange(nextValue),
    1000
  );

  const handleChange = (event) => {
    const { value: nextValue } = event.target;
    setValue(nextValue);
    debouncedSave(nextValue);
  };

  return props.renderProps({ ...props.inputProps, onChange: handleChange, value });

}
  • Since you are passing the props and the component also from the same place, you can do it in a simple way like below由于您也是从同一个地方传递propscomponent ,因此您可以通过如下简单的方式进行
 <DebouncedInput
    initialValue={value}
    onChange={handleChange}
    renderProps={(props) => <TextArea {...props} rows={5} cols={50}/>}
  />
function DebouncedInput(props) {
  const [value, setValue] = useState(props.initialValue);
  const debouncedSave = useDebounce(
    (nextValue) => props.onChange(nextValue),
    1000
  );

  const handleChange = (event) => {
    const { value: nextValue } = event.target;
    setValue(nextValue);
    debouncedSave(nextValue);
  };

  return props.renderProps({ onChange: handleChange, value });

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 props 传递给组件:“对象作为 React 子对象无效” - Pass props to component: “Objects are not valid as a React child” 对象在日期道具上作为 React 子项无效 - Objects are not valid as a React child on a date props 渲染后修改React Child Component道具 - Modify React Child Component props after render react render-对象作为React子对象无效 - react render - objects are not valid as a React child React render:对象作为React子对象无效 - React render: Objects are not valid as a React child 对象在将道具从一个组件传递到另一个组件时,不能作为反应子对象(带有键的找到的对象)有效 - objects are not valid as a react child (found object with keys) while passing props from one component to other Nextjs:在对象数组上使用 map 时无法呈现组件。 对象作为 React 子项无效 - Nextjs: Cant render a component while using map over a array of objects. Objects are not valid as a React child 尝试基于 AsyncStorage React-Native 渲染组件时,对象作为反应子对象无效 - objects are not valid as a react child when trying to render component based off of AsyncStorage React-Native 将 function 提取到独立的 reactjs 组件中会引发 Objects are not valid as a React child (found: [object Window]) - extracting a function into standalone reactjs component throws Objects are not valid as a React child (found: [object Window]) 当子道具动态变化时,对象作为 React 子对象无效 - Objects are not valid as a React child when child props change dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM