Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.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
Reactjs 吊钩的破坏方案_Reactjs_React Hooks - Fatal编程技术网

Reactjs 吊钩的破坏方案

Reactjs 吊钩的破坏方案,reactjs,react-hooks,Reactjs,React Hooks,对于这个accordin.js组件,为什么这行可以工作setActiveIndex(index)我以为我们把东西[1]分配给了对象setActiveIndex,那么为什么我们能够像调用函数一样调用对象setActiveIndex呢 import React, { useState } from 'react'; const Accordion = ( { items }) => { // const [activeIndex, setActiveIn

对于这个accordin.js组件,为什么这行可以工作
setActiveIndex(index)我以为我们把东西[1]分配给了对象setActiveIndex,那么为什么我们能够像调用函数一样调用对象setActiveIndex呢

        import React, { useState } from 'react';

    const Accordion = ( { items }) => {
       // const [activeIndex, setActiveIndex] = useState(null);
        const things = useState(null);
        const activeIndex = things[0];
        const setActiveIndex = things[1];
        
        const onTitleClick = (index) => {
        console.log('Title clicked', index);
        setActiveIndex(index);
        }
        const renderedItems = items.map((item, index) => {
        return (
        <React.Fragment key={item.title}>
            <div 
            className="title active"
            onClick={  () => onTitleClick(index)}
            >
            <i className="dropdown icon"></i>
                {item.title}
               
            </div>
            <div className="content active">
            <p>{item.content}</p>
            </div>
        </React.Fragment>)
        });
        return (<div className="ui styled accordion">
        {renderedItems}
        <h1>{activeIndex}</h1>
        </div>);
    };


    export default Accordion;
import React,{useState}来自“React”;
常数手风琴=({items})=>{
//常量[activeIndex,setActiveIndex]=useState(null);
const things=useState(null);
const activeIndex=things[0];
const setActiveIndex=things[1];
const onTitleClick=(索引)=>{
console.log('点击标题',索引);
setActiveIndex(索引);
}
const renderItems=items.map((项,索引)=>{
返回(
onTitleClick(索引)}
>
{item.title}
{item.content}

) }); 返回( {renderItems} {activeIndex} ); }; 导出默认手风琴;
useState钩子返回一个包含两个元素的数组。 第一个元素是该状态的当前值。 第二个元素是更新状态值的函数

编写此代码的惯用方法是
const[activeIndex,setActiveIndex]=useState(null)


在accordio code
中,things
实际上是一个包含上述两个值的数组。因此,
things[1]
是函数
setActiveIndex

useState钩子返回一个包含两个元素的数组。 第一个元素是该状态的当前值。 第二个元素是更新状态值的函数

编写此代码的惯用方法是
const[activeIndex,setActiveIndex]=useState(null)

在accordio code
中,things
实际上是一个包含上述两个值的数组。因此,
things[1]
是函数
setActiveIndex

根据:

useState返回什么?它返回一对值:当前状态和更新它的函数。这就是为什么我们编写
const[count,setCount]=useState()
。这类似于类中的
This.state.count
This.setState
,只不过它们是成对的。如果您不熟悉我们使用的语法,我们将在本页底部返回

更惯用的版本可能如下所示:

 const [activeIndex, setActiveIndex] = useState(); // null by default if not set
这将取代这些行,完全消除
内容

const things = useState(null);
const activeIndex = things[0];
const setActiveIndex = things[1];
根据:

useState返回什么?它返回一对值:当前状态和更新它的函数。这就是为什么我们编写
const[count,setCount]=useState()
。这类似于类中的
This.state.count
This.setState
,只不过它们是成对的。如果您不熟悉我们使用的语法,我们将在本页底部返回

更惯用的版本可能如下所示:

 const [activeIndex, setActiveIndex] = useState(); // null by default if not set
这将取代这些行,完全消除
内容

const things = useState(null);
const activeIndex = things[0];
const setActiveIndex = things[1];