Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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 classic'class'组件中使用React挂钩?_Javascript_Reactjs_React Hooks - Fatal编程技术网

Javascript 如何在React classic'class'组件中使用React挂钩?

Javascript 如何在React classic'class'组件中使用React挂钩?,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,在本例中,我有一个react类: class MyDiv extends React.component constructor(){ this.state={sampleState:'hello world'} } render(){ return <div>{this.state.sampleState} } } 类MyDiv扩展React.component 构造函数(){ this.state={sampleState:'he

在本例中,我有一个react类:

class MyDiv extends React.component
   constructor(){
      this.state={sampleState:'hello world'}
   }
   render(){
      return <div>{this.state.sampleState}
   }
}
类MyDiv扩展React.component
构造函数(){
this.state={sampleState:'hello world'}
}
render(){
返回{this.state.sampleState}
}
}

问题是我是否可以在这上面加上React钩子。我知道React钩子是React类风格的替代品。但是如果我想慢慢地迁移到React钩子中,我可以将有用的钩子添加到类中吗?

类组件不支持钩子-

根据报告:

不能在类组件内部使用钩子,但可以在一棵树中将类和函数组件与钩子混合使用。组件是使用挂钩的类还是函数是该组件的实现细节。从长远来看,我们希望钩子是人们编写组件的主要方式


使用现有的类组件是不可能的。您必须将类组件转换为功能组件,然后执行以下操作-

function MyDiv() {
const [sampleState, setSampleState] = useState('hello world');
return (
      <div>{sampleState}</div>
    )
}
函数MyDiv(){
const[sampleState,setSampleState]=useState(“hello world”);
返回(
{sampleState}
)
}

钩子不是用于类,而是用于函数。如果希望使用钩子,可以从编写新代码开始,将其作为带有钩子的功能组件

不能在类组件内部使用挂钩,但可以 明确地将类和函数组件与挂钩混合在一个 树。组件是一个类还是一个使用钩子的函数是不确定的 该组件的实现细节。从长远来看,我们 期望钩子是人们编写组件的主要方式

constmydiv=()=>{
const[sampleState,setState]=useState(“hello world”);
render(){
返回{sampleState}
}
}

正如其他答案所解释的,hooks API旨在为功能组件提供当前仅在类组件中可用的功能。钩子不应该在类组件中使用

可以编写类组件,以便更容易地迁移到功能组件

对于单一状态:

class MyDiv extends Component {
   state = {sampleState: 'hello world'};

   render(){
      const { state } = this;
      const setState = state => this.setState(state);

      return <div onClick={() => setState({sampleState: 1})}>{state.sampleState}</div>;
   }
}
转换为

const MyDiv = () => {
   const [state, setState] = useState({sampleState: 'hello world'});

   return <div onClick={() => setState({sampleState: 1})}>{state.sampleState}</div>;
}
const MyDiv = () => {
   const [sampleState, setSampleState] = useState('hello world');

   return <div onClick={() => setSampleState(1)}>{sampleState}</div>;
}
constmydiv=()=>{
const[sampleState,setSampleState]=useState(“hello world”);
返回setSampleState(1)}>{sampleState};
}
我们一直在做这类事情,直到挂钩出现。您可以为钩子编写一个简单的高阶组件包装器

function withMyHook(Component) {
  return function WrappedComponent(props) {
    const myHookValue = useMyHook();
    return <Component {...props} myHookValue={myHookValue} />;
  }
}
带有myhook(组件)的函数{
返回函数WrappedComponent(props){
const myHookValue=useMyHook();
返回;
}
}
虽然这并不是真正从类组件直接使用钩子,但这至少允许您从类组件使用钩子的逻辑,而无需重构

class MyComponent extends React.Component {
  render(){
    const myHookValue = this.props.myHookValue;
    return <div>{myHookValue}</div>;
  }
}

export default withMyHook(MyComponent);
类MyComponent扩展了React.Component{ render(){ const myHookValue=this.props.myHookValue; 返回{myHookValue}; } } 使用myhook导出默认值(MyComponent);
有状态组件、容器或基于类的组件都支持React钩子的功能,因此我们不需要在有状态组件中只在无状态组件中使用React钩子

一些附加信息

什么是反应挂钩? 那么什么是钩子呢?钩子是一种新的方式,或者为我们提供了一种编写组件的新方式

到目前为止,我们当然有基于函数和类的组件,对吗?功能组件接收道具,然后返回一些应该呈现到屏幕上的JSX代码

它们非常适合于表示,因此用于呈现UI部分,而不是业务逻辑,它们通常专注于每个组件的一个或几个目的

另一方面,基于类的组件也将接收道具,但它们也具有这种内部状态。因此,基于类的组件实际上是包含大部分业务逻辑的组件,因此对于业务逻辑,我指的是我们发出HTTP请求,我们需要处理响应并更改应用程序的内部状态,甚至可能没有HTTP。用户填写表单,我们希望在屏幕上的某个地方显示它,我们需要状态,我们需要基于类的组件,因此我们通常也使用基于类的组件来编排其他组件,并将状态作为道具传递给功能组件

现在,这种分离存在一个问题,它增加了所有好处,但我们存在的一个问题是,从一种组件形式转换到另一种组件形式很烦人。这不是很难,但很烦人

如果您发现自己处于需要将函数组件转换为基于类的组件的情况下,那么需要进行大量的键入,而且键入的内容总是相同的,因此这很烦人

引号中一个更大的问题是生命周期挂钩可能很难正确使用

显然,添加componentDidMount并在其中执行一些代码并不困难,但知道要使用哪个生命周期挂钩、何时以及如何正确使用它,这可能是一个挑战,尤其是在更复杂的应用程序中,无论如何,如果我们有一种创建组件的方法,并且超级组件可以处理HTTP请求等状态和副作用,还可以呈现用户界面,这不是很好吗


嗯,这正是挂钩的意义所在。钩子为我们提供了一种创建功能组件的新方法,这一点很重要。

React钩子允许您在不编写类的情况下使用React功能和生命周期。 它类似于类组件的等效版本,具有更小且可读的形状因子。您应该迁移到React钩子,因为编写它很有趣。 但您不能在类组件内编写react钩子,因为它是为功能组件引入的

这可以很容易地转换为:

class MyDiv extends React.component
   constructor(){
      this.state={sampleState:'hello world'}
   }
   render(){
      return <div>{this.state.sampleState}
   }
}

const MyDiv = () => {
   const [sampleState, setSampleState] = useState('hello world');
   return <div>{sampleState}</div>
}
cl
class MyComponent extends React.Component {
  render(){
    const myHookValue = this.props.myHookValue;
    return <div>{myHookValue}</div>;
  }
}

export default withMyHook(MyComponent);
class MyDiv extends React.component
   constructor(){
      this.state={sampleState:'hello world'}
   }
   render(){
      return <div>{this.state.sampleState}
   }
}

const MyDiv = () => {
   const [sampleState, setSampleState] = useState('hello world');
   return <div>{sampleState}</div>
}
constructor(props) {
      super(props);
      this.myRef = React.createRef();
   }

...


<FunctionComponent ref={this.myRef} />
class MyDiv extends React.Component {
  render() {
    return (
      <HookWrapper
        // pass state/props from inside of MyDiv to Hook
        someProp={42} 
        // process Hook return value
        render={hookValue => <div>Hello World! {hookValue}</div>} 
      />
    );
  }
}

function HookWrapper({ someProp, render }) {
  const hookValue = useCustomHook(someProp);
  return render(hookValue);
}
function HookWrapper({ someProp }) {
  useCustomHook(someProp);
  return null;
}

// ... usage
<HookWrapper someProp={42} />
import withComponentHooks from 'with-component-hooks';


class MyComponent extends React.Component {

    render(){
        const props = this.props;
        const [counter, set] = React.useState(0);

        //TODO...

    }
}

export default withComponentHooks(MyComponent)