Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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 在react中填充useState之前清除数组_Reactjs_React Hooks - Fatal编程技术网

Reactjs 在react中填充useState之前清除数组

Reactjs 在react中填充useState之前清除数组,reactjs,react-hooks,Reactjs,React Hooks,如何清除数组并使用挂钩立即填充react(useState) const[log,setLog]=useState([]); const onSubmit=data=>{ 设num=parseInt(data.num); setLog([]); 对于(设i=1;i为什么要执行setLog([])然后执行setLog([…log,]); 如果您想setLog而不使用以前的log内容,只需执行以下操作: const [log, setLog] = useState([]); const onSub

如何清除数组并使用挂钩立即填充react(useState)

const[log,setLog]=useState([]);
const onSubmit=data=>{
设num=parseInt(data.num);
setLog([]);

对于(设i=1;i为什么要执行
setLog([])
然后执行
setLog([…log,]);

如果您想
setLog
而不使用以前的
log
内容,只需执行以下操作:

const [log, setLog] = useState([]);

const onSubmit = data => {
  let num = parseInt(data.num);
  for (let i = 1; i <= num; i++) {
    axios
      .get(data.url)
      .then(function(response) {
        setLog([{ type: "ok", message: "Good" }]);
      })
      .catch(function(error) {
        setLog([{ type: "error", message: "Bad" }]);
      });
  }
};
const[log,setLog]=useState([]);
const onSubmit=data=>{
设num=parseInt(data.num);
for(设i=1;i{
设num=parseInt(data.num);
setLog([]);
for(设i=1;i{
push({type:“ok”,message:“Good”});
返回{prevLog};
});
})
.catch(函数(错误){
设置状态(prevLog=>{
push({type:“error”,message:“Bad”});
返回{prevLog};
});
});
}
};

我认为这会奏效:

const [log, setLog] = useState([]);

const onSubmit = data => {
  let num = parseInt(data.num);

  for (let i = 1; i <= num; i++) {
    axios
      .get(data.url)
      .then(function(response) {
        setLog([{ type: "ok", message: "Good" }]);
      })
      .catch(function(error) {
        setLog([{ type: "error", message: "Bad" }]);
      });
  }
};
const[log,setLog]=useState([]);
const onSubmit=data=>{
设num=parseInt(data.num);
对于(设i=1;i不受欢迎的意见:

使用类组件。它提供当前状态的访问器。

class SomeComponent extends Component {
  state = {
    isSubmitting: false,
    logs: []
  }

  async submit(data){
    const num = data.num;
    if (this.isSubmitting) {
      return;
    }

    let logs = [];
    this.setState({ logs, isSubmitting: true });

    for (const i = 0; i < num; i++) {
      await axios.get(data.url)
        .then(() => {
          logs = logs.concat({ type: "ok", message: "Good" });
          this.setState({ logs });
        })
        .catch(() => {
          logs = logs.concat({ type: "error", message: "Bad" })
          this.setState({ logs })
        })
    }

    this.setState({ isSubmitting: false });
  }

  render(){
    return (
      <form onSubmit={() => this.submit(getSomeData())}></form>
    )
  }
}


class SomeComponent扩展组件{
状态={
提交:错误,
日志:[]
}
异步提交(数据){
const num=data.num;
如果(本文件提交){
返回;
}
让logs=[];
this.setState({logs,isSubmitting:true});
for(常数i=0;i{
logs=logs.concat({type:“ok”,message:“Good”});
this.setState({logs});
})
.catch(()=>{
logs=logs.concat({type:“error”,message:“Bad”})
this.setState({logs})
})
}
this.setState({isSubmitting:false});
}
render(){
返回(
this.submit(getSomeData())}>
)
}
}
//如果希望并行执行请求,请使用此选项
异步提交(数据){
const num=data.num;
如果(本文件提交){
返回;
}
让logs=[];
this.setState({logs,isSubmitting:true});
常量请求=[]
for(常数i=0;i{
logs=logs.concat({type:“ok”,message:“Good”});
this.setState({logs});
})
.catch(()=>{
logs=logs.concat({type:“error”,message:“Bad”})
this.setState({logs})
})
)
}
等待承诺。所有(请求)
this.setState({isSubmitting:false});
}

no,因为不仅仅是一个请求,可能会有很多请求,我必须保存所有响应。@toledorobia好的,我想我理解你的需要,我编辑了答案,请尝试新的代码no,因为不仅仅是一个请求,可能会有很多请求,我必须保存所有响应,并在每次提交时清除日志
const [log, setLog] = useState([]);

const onSubmit = data => {
  let num = parseInt(data.num);

  for (let i = 1; i <= num; i++) {
    axios
      .get(data.url)
      .then(function(response) {
        setLog([{ type: "ok", message: "Good" }]);
      })
      .catch(function(error) {
        setLog([{ type: "error", message: "Bad" }]);
      });
  }
};
class SomeComponent extends Component {
  state = {
    isSubmitting: false,
    logs: []
  }

  async submit(data){
    const num = data.num;
    if (this.isSubmitting) {
      return;
    }

    let logs = [];
    this.setState({ logs, isSubmitting: true });

    for (const i = 0; i < num; i++) {
      await axios.get(data.url)
        .then(() => {
          logs = logs.concat({ type: "ok", message: "Good" });
          this.setState({ logs });
        })
        .catch(() => {
          logs = logs.concat({ type: "error", message: "Bad" })
          this.setState({ logs })
        })
    }

    this.setState({ isSubmitting: false });
  }

  render(){
    return (
      <form onSubmit={() => this.submit(getSomeData())}></form>
    )
  }
}


// use this if you want the request to be executed parallely

  async submit(data){
    const num = data.num;
    if (this.isSubmitting) {
      return;
    }

    let logs = [];
    this.setState({ logs, isSubmitting: true });

    const requests = []
    for (const i = 0; i < num; i++) {
      requests.push(
        axios.get(data.url)
          .then(() => {
            logs = logs.concat({ type: "ok", message: "Good" });
            this.setState({ logs });
          })
          .catch(() => {
            logs = logs.concat({ type: "error", message: "Bad" })
            this.setState({ logs })
          })
      )
    }

    await Promise.all(requests)

    this.setState({ isSubmitting: false });
  }