我正在尝试为反应应用程序创建一个用户区域,
如果用户已登录,则数据将存储在 redux 中的“userData”下,
我遇到的问题是,如果用户在未登录时对用户区域进行操作,我希望应用程序将他们重定向到另一个“notloggedin”页面,
我有以下内容,但是我遇到的问题是,当首次渲染路线时userDetails
返回一个空对象,该对象随后会触发history.push('/notloggedin')
. 我可以看到,如果我第一次删除history.push('/notloggedin')
并加载组件,它会userDetails
作为空对象加载,但随后会重新呈现为填充的对象。我真的很困惑如何仅在从 useSelector 填充对象后才使组件呈现。
import React, {useState, useEffect} from 'react';
import {useSelector, shallowEqual} from 'react-redux';
const index = () => {
const userDetails = useSelector(state=>state.userData, shallowEqual);
useEffect(()=>{
if(Object.keys(userDetails).length===0){
history.push('/notloggedin')
}
},[])
return (
<div>
<h1>Member Area</h1>
<p>Your User name us</p>
{userDetails.username}
...
</div>
);
};
export default index;
Run Code Online (Sandbox Code Playgroud)
如果我删除该行const userDetails = useSelector(state=>state.userData, shallowEqual);
,那么该组件只会按照我的预期渲染一次,我不明白为什么上面的 live 会导致它渲染两次!
userData 使用以下操作填充
//login/actions.js
export const getUserData = (token) => { …
Run Code Online (Sandbox Code Playgroud) 我正在使用 graphql 使用 Shopify API,我们的任务是实现 srcset 图像以加快页面加载时间,但是我在生成将拉取多个大小网址的查询时遇到问题。
query ($tag: String!) {
products(first: 10, query: $tag) {
edges {
cursor
node {
id
tags
handle
images(first:1, maxWidth:360) {
edges {
node {
src
__typename
}
__typename
}
__typename
}
}
__typename
}
__typename
}
}
Run Code Online (Sandbox Code Playgroud)
这个查询的作用是拉入最大宽度为 360px 的单个图像 url,但是如果我执行类似于下面的操作(我希望生成 360px 图像和 720px 图像的 url,那么我会收到错误 "message": "Field 'images' has an argument conflict: {first:\"1\",maxWidth:\"360\"} or {first:\"1\",maxWidth:\"720\"}?",
query ($tag: String!) {
products(first: 10, query: $tag) {
edges {
cursor
node {
id
tags
handle …
Run Code Online (Sandbox Code Playgroud) 我有一个采用以下格式的字符串(请注意,每个单词之间和每一行开头之间的随机数都是空格):
1。英国航空15FEB伦敦香港2。国泰航空01MAR HONGKONG SHANGHAI
3。澳洲航空12MAR澳大利亚悉尼
我希望输出是
1。英国航空15FEB伦敦香港 2。国泰航空01MAR HONGKONG SHANGHAI 3。澳洲航空12MAR澳大利亚悉尼
我有以下代码
$flightinfo = preg_replace('/\h+/', ' ', $flightinfo);
$flightinfo = trim($flightinfo);
$flightinfo = str_replace("\r\n\r\n","\r\n",$flightinfo);/* checks and removes double line breaks if they're there.
Run Code Online (Sandbox Code Playgroud)
但这给出了以下内容,这几乎是正确的,但是在第2行和第3行的开头是空白
1。英国航空15FEB伦敦香港 2。国泰航空01MAR HONGKONG SHANGHAI 3。澳洲航空12MAR澳大利亚悉尼
任何人都知道如何在第2行和第3行的开头删除空白
我正在尝试将 FFMpeg 集成到 Laravel 项目中,但在调用端点时收到错误:
FFMpeg\Exception\ExecutableNotFoundException: Unable to load FFMpeg in file /Users/me/Desktop/video/vendor/php-ffmpeg/php-ffmpeg/src/FFMpeg/Driver/FFMpegDriver.php on line 55
我做了什么:
brew install ffmpeg
- 在本地安装了FFMEG,使用终端时可以确认这是否有效
全新 Laravel 安装composer create-project laravel/laravel example-app
安装 php ffmpegcomposer require php-ffmpeg/php-ffmpeg
安装 Laravel ffmpegcomposer require pbmedia/laravel-ffmpeg
将 FFMPEG 添加到提供者和别名中 app.php
:
'providers' => [
...
ProtoneMedia\LaravelFFMpeg\Support\ServiceProvider::class,
...
];
'aliases' => [
...
'FFMpeg' => ProtoneMedia\LaravelFFMpeg\Support\FFMpeg::class
...
];
Run Code Online (Sandbox Code Playgroud)
然后我的控制器是
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use FFMpeg;
class VideoController extends Controller
{
public function makeVideo()
{
FFMpeg::fromDisk('songs') …
Run Code Online (Sandbox Code Playgroud) php ×2
axios ×1
ffmpeg ×1
graphql ×1
laravel ×1
react-hooks ×1
react-redux ×1
reactjs ×1
redux ×1
regex ×1
shopify ×1
str-replace ×1
string ×1
video ×1