简体   繁体   English

我将 JSS 样式放在自定义 React 钩子中。 这很糟糕吗?

[英]I put JSS styles inside a custom React hook. Is this bad?

I've been struggling to find a way to make a solid reusable SearchBar Component.我一直在努力寻找一种方法来制作一个可靠的可重用 SearchBar 组件。 I tried to make a component for it.我试图为它制作一个组件。 However this component cannot hold its own state.然而这个组件不能保持它自己的状态。 This means you would have two imports to use this search bar.这意味着您将有两个导入来使用此搜索栏。 Something like this:像这样的东西:

import React, { useState } from 'react';
import SearchBar from './SearchBar';

const Parent = () => {
   const [value, setValue] = useState("");

   return (
      <div>
         <SearchBar ...use 'value' and 'setValue' somehow... blah blah... />
         {list.map((item) => ...render item if it matches 'value'...)}
      </div>

}

That's easy enough but my brain go going... I found a "useInput" custom hook on the internet that uses the spread syntax to pass some props to an 'input' tag... I decided to see just how far I could take this.这很容易,但我的大脑继续前进......我在互联网上找到了一个“useInput”自定义钩子,它使用传播语法将一些道具传递给“输入”标签......我决定看看我能走多远这个。

As it turns out we are using JSS in our project and this is when things got a little ridiculous.事实证明,我们在我们的项目中使用了JSS ,这让事情变得有点荒谬。 Here is my Hook:这是我的钩子:

import { useState } from 'react';
import { createUseStyles } from 'react-jss';

// My gut tells my this is a really terrible thing to do! 
export const useSearchBar = () => {
    const [value, setValue] = useState('');
    const classes = useStyles();
    return {
        value,
        setValue,
        reset: () => setValue(""),
        bindSearchBar: {
            type: 'text',
            className: classes.searchBar,
            placeholder: 'Search',
            value,
            onChange: (event: {target: {value: string}}) => {
                setValue(event.target.value);
            }
        }
    };
};

const useStyles = createUseStyles({
    searchBar: {
        ...search bar styles...
    },
});

The simplest implementation looks like this:最简单的实现如下所示:

import React form 'react';
import { useSearchBar } from '../hooks/useSearchBar';

const Component = () => {

   const { bindSearchBar, value } = useSearchBar();

   return <input {...bindSearchBar} />;

}

I would need a list but I think you get the point.我需要一个清单,但我认为你明白了。 There is this weird feeling I have that there is something terribly wrong with this.我有一种奇怪的感觉,认为这有什么可怕的地方。 I'm pretty sure I'll love it right now but in 6 months I'm going to hate my self.我很确定我现在会喜欢它,但 6 个月后我会讨厌自己。 Can anyone tell me what might go wrong here or why this might be bad practice?谁能告诉我这里可能出了什么问题,或者为什么这可能是不好的做法? Or do you think using hooks this way is somewhat viable?或者你认为以这种方式使用钩子有点可行?

There is nothing wrong to create jss classes in a hook.在钩子中创建 jss 类没有错。 The useStyles is a hook and the attribute of a custom hook is to use other hooks inside it. useStyles 是一个钩子,自定义钩子的属性是在其中使用其他钩子。

Other than that, here is a performance tip.除此之外,这是一个性能提示。

export const useSearchBar = () => {
    const [value, setValue] = useState('');
    const classes = useStyles();
    
    //wrap with a useMemo to prevent recreation of the props when the container rerender
    const searchBarProps = React.useMemo(()=>({
        value,
        setValue,
        reset: () => setValue(""),
        bindSearchBar: {
            type: 'text',
            className: classes.searchBar,
            placeholder: 'Search',
            value,
            onChange: (event: {target: {value: string}}) => {
                setValue(event.target.value);
            }
        }
     }),[value, classes]);  

    return searchBarProps;
};

暂无
暂无

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

相关问题 React Custom Hooks - 将所有责任委托给自定义钩子。 反模式? - React Custom Hooks - Delegating all the responsibilities to a custom hook. Anti-pattern? 如何将 Heroes 数组更改为 res 数组? 我正在使用 react useState 钩子。 这适用于以前的项目,但不适用于这里 - How do I change the Heroes array, to the res array? I am using the react useState hook. This has worked on previous projects but not here 在React组件中使用JSS合并样式 - Merge styles using JSS in react component 使用 useState 挂钩时,我的 2D 网格不会重新呈现。 有没有办法在我改变里面元素的 object 属性后重新渲染? - my 2D grid does not re-render while using with useState hook. Is there any way to re-render after I change object attributes of the elements inside? 反应 useContext() 性能,自定义钩子内的 useContext - React useContext() performance, useContext inside custom hook react jss 如何设置自定义 html 标签的样式 - react jss how to style custom html tag 在 React JS 的回调中使用自定义钩子 - Use custom hook inside a callback in React JS useState 在自定义反应挂钩中不起作用 - useState not working inside a custom react hook React js useState 钩子。 单击复选框时如何使用其中的数组更新 json 对象的状态 - React js useState hook. How to update state of a json object with an a array in it when a checkbox is clicked 反应:在 useEffect 挂钩中的网络请求后更新然后访问 state。 State 仍然过时 - React : Updating and then accessing state after a network request in useEffect hook. State remains stale
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM