使用钩子时,是否有一种简单的方法可以将父组件的所有道具传递给子组件?
似乎将每个状态重复发送到每个组件。
我想知道扩频运算符是否有效,正确的语法是什么?
我很抱歉没有提供示例,不得不离开。这个例子就是我正在做的事情。
const Parent = () => {
const [count, setcount] = useState(0)
const [example, setExample] = useState('')
return (
<Child props={...How to pass all props in the state}
)
}
答案 0 :(得分:0)
它与react hooks无关。
这只是JavaScript,因此您可以使用spread syntax:
backgroundBlob := storageClient.Bucket(inputBucket).Object(background)
backgroundImg, err := backgroundBlob.NewReader(ctx)
if err != nil {
return "", fmt.Errorf("backgroundImg: %v", err)
}
foregroundBlob := storageClient.Bucket(inputBucket).Object(foreground)
foregroundImg, err := foregroundBlob.NewReader(ctx)
if err != nil {
return "", fmt.Errorf("foregroundImg: %v", err)
}
outputBlob := storageClient.Bucket(outputBucket).Object(output)
outputImg := outputBlob.NewWriter(ctx)
defer outputImg.Close()
// Set up some additional file handles since we're dealing with more inputs than STDIN Can cope with
cmd := exec.Command("convert", "fd:3", "fd:4", "-composite", "fd:5")
cmd.ExtraFiles = append(cmd.ExtraFiles,backgroundImg)
cmd.ExtraFiles = append(cmd.ExtraFiles,foregroundImg)
cmd.ExtraFiles = append(cmd.ExtraFiles,outputImg)
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("cmd.Run: %v", err)
}
log.Printf("Blurred image has been uploaded to %s", outputBlob.ObjectName())
return outputBlob.ObjectName(), nil
}```
So, from where I am at the moment, I need to do one of the following two things:
1. Figure out how to get Google's storage API to treat/use objects in their storage buckets as "os.File"s
OR
2. Figure out how to execute my "convert" command using multiple Readers as input, rather than leveraging the ExtraFiles array.
If anybody out there has any ideas on how to achieve either of the above, or has alternate ways of solving this problem, I would be very grateful to hear them!
答案 1 :(得分:0)
是的,您可以使用上下文api。出于相同的目的实现了此目的(当将道具传递到子组件时)。
https://reactjs.org/docs/context.html
此外,您可以执行以下操作:
<SomeComponent {...this.props} />