Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React:如何向无状态功能子组件提供回调箭头函数?_Javascript_Reactjs_Properties_Callback_Functional Programming - Fatal编程技术网

Javascript React:如何向无状态功能子组件提供回调箭头函数?

Javascript React:如何向无状态功能子组件提供回调箭头函数?,javascript,reactjs,properties,callback,functional-programming,Javascript,Reactjs,Properties,Callback,Functional Programming,我正在努力为无状态函数子组件提供回调函数。我有以下两个部分: export const FrontDeskGUI = () => { const callback = (event) => console.log(event.target.value); return ( <Col xs={12}> <Panel collapsible defaultExpanded header="Empfang">

我正在努力为无状态函数子组件提供回调函数。我有以下两个部分:

export const FrontDeskGUI = () => {
    const callback = (event) => console.log(event.target.value);
    return (
        <Col xs={12}>
            <Panel collapsible defaultExpanded header="Empfang">
                <ButtonGrid/>
            </Panel>
            <Panel collapsible defaultExpanded header="Verwaltung">
                <ButtonGrid/>
            </Panel>
            <CommandInput callback={callback}/>
        </Col>);
};

export const CommandInput = (callback) => {
    const style = {
        textTransform: 'uppercase'
    };
    return (
        <Col xs={12}>
            <form>
                <FormGroup
                    controlId="command">
                    <FormControl
                        type="text"
                        style={style}
                        placeholder="K&uuml;rzel eingeben"
                        onChange={callback}/>
                </FormGroup>
            </form>
        </Col>);
};
export const FrontDeskGUI=()=>{
const callback=(event)=>console.log(event.target.value);
返回(
);
};
export const CommandInput=(回调)=>{
常量样式={
textTransform:'大写'
};
返回(
);
};
在渲染过程中,我遇到以下错误:

警告:表单propType失败:提供给
输入的
类型的
对象
的属性
一旦更改
无效,应为
功能
。检查
FormControl
的渲染方法

每次我在文本输入中输入内容时,都会出现以下错误:

未捕获类型错误:inputProps.onChange.call不是函数 在Object.executeOnChange(LinkedValueUtils.js:132)


在一个无状态的环境中,这可能吗?从技术上讲,提供的回调函数是常量,因此CommandInput组件中没有固有的状态。我已经看到了一些关于将函数绑定到正确的this指针的答案,但如果可能的话,我希望避免这种情况。

SFC收到的单个参数是一个properties对象,其中JSX标记中组件上使用的每个属性都有一个属性

因此,接受该属性并使用其
回调
属性:

export const CommandInput = (props) => {
    // ...use props.callback...
}
或使用参数分解:

export const CommandInput = ({callback}) => {
                          // ^--------^---------------- note { }
    // ... use callback...
}
目前,您正试图使用props对象本身作为
onChange
,这就是为什么会出现错误

使用分解结构的示例:

export const CommandInput = ({callback}) => {
                          // ^--------^---------------- note { }
    // ... use callback...
}
const示例=({callback})=>
点击我;
ReactDOM.render(
console.log(“单击”)}/>,
document.getElementById(“根”)
);

这正是我想要的,很有魅力。我想知道,如果有多个参数,这是如何工作的,但这就清楚了。非常感谢。