简体   繁体   English

可以在反应组件之外使用handleChange function 吗?

[英]Possible to use handleChange function outside of react component?

I'm using a simple stateless function as a react component with a react hook state.我正在使用一个简单的无状态 function 作为带有反应钩子 state 的反应组件。 Also I would like to keep every function in a separated place to make testing easier and to make the code more readable if the function gets a bit bigger.另外,如果 function 变得更大一点,我还想将每个 function 放在一个单独的地方,以使测试更容易并使代码更具可读性。

So in this example the handleChange function should do a bit more then just set the state value.所以在这个例子中, handleChange function 应该做的更多,然后只是设置 state 值。 But if I put it outside of the function, I do not have access to the setMessage function and the props of the component.但是如果我把它放在 function 之外,我就无法访问setMessage function 和组件的 props。

Is this anyway useful or is it a wrong react way?这无论如何有用还是错误的反应方式?

example.js例子.js

import React, { useState } from 'react'

export function handleChange (event) = {
    // do something...
    // how to get props?
    setMessage('new'); // is not defined at this place
}

export default function Message = (props) => {
    const [message, setMessage] = useState('');

    return (
        <input
            type="text"
            value={message}
            placeholder="Enter a message"
            onChange={handleChange}
        />
    );
};

You can use currying for this您可以为此使用柯里化

export const handleChange = setState => event => {
  setState('new'); 
}

and in the component并在组件中

export default function Message = (props) => {
const [message, setMessage] = useState('');
const reactOnChange = useCallback(handleChange(setMessage),[]);
return (
    <input
        type="text"
        value={message}
        placeholder="Enter a message"
        onChange={reactOnChange }
    />
  );
};

Alternatively:或者:

export const handleChange = relevantData => {
  return 'new'; 
}


export default function Message = (props) => {
const [message, setMessage] = useState('');
return (
    <input
        type="text"
        value={message}
        placeholder="Enter a message"
        onChange={e => setMessage(handleChange(getRelevantDataFrom(e)))}
    />
  );
};

This gives a function a total separation from react.这使 function 与反应完全分离。

Nesting functions inside functions does not look very well.在函数内部嵌套函数看起来不太好。 If you like functional approach you can pipe them如果你喜欢功能方法,你可以 pipe 他们

   onChange={pipe(getRelevantDataFrom, handleChange, setMessage)}

where在哪里

const pipe = (...functions) => x => functions.reduce((acc, f) => f(acc), x)

I can see and agree that separating your logic from your component can be beneficial for testing (though sometimes testing the component itself is also valuable so this is not a hard rule by any means).我可以看到并同意将逻辑与组件分开可能有利于测试(尽管有时测试组件本身也很有价值,因此这无论如何都不是硬性规则)。

In your case, I think you're trying to draw the line a little to far away from React.就您而言,我认为您正试图将这条线与 React 划清界限。 What I would suggest is to still have "handleChange" inside of the component, but create a new function outside of the component that handles the logic you want to isolate and that returns the message to set at the end.我的建议是在组件内部仍然有“handleChange”,但在组件外部创建一个新的 function 来处理您要隔离的逻辑并返回要在最后设置的消息。 This function can take the props as a argument.这个 function 可以把 props 作为参数。 So your handleChange function would take the relevant props and pass them as arguments to this function, then call setMessage with the return value of your function. So your handleChange function would take the relevant props and pass them as arguments to this function, then call setMessage with the return value of your function. Now you have a pure input/output function that you can test.现在您有了可以测试的纯输入/输出 function。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM