score:-1
I went through a similar issue. Luckily I found this to work with string interpolations:
// Option 1
$postName
const query1 = `file(relativePath: {eq: $postName}) { ... }`
// Option 2
${({postName})
const query2 = `file(relativePath: {eq: ${({postName})}) { ... }`
Have a look at a snippet of code I used for one of my projects: https://github.com/timrodz/.com/blob/master/src/components/common/Button/index.js#L33
score:4
Short answer: You can't.
Follow the discussion on the github repo. If you scroll all the way down, it looks like it will be available in the next few releases.
Workaround
- Query for all the data.
- Filter for the specific data you need. You can use variables in array functions.
Here an implementation with gatsby-image
that I use in my project
const Page = (props) => {
const { data: { allFile: { edges } } } = props;
const oneImage = edges.filter(edge => // filter with your variable
edge.node.childImageSharp.fluid.originalName === props.yourVARIABLE)[0].node.childImageSharp.fluid;
{/* ... */}
}
export const query = graphql`
// ...
EDIT
In your comment, you made a mistake destructuring your props in the parameters of your arrow function. This is your revised code:
const BackgroundDiv = (props) => {
// destructuring all the props to make it clear
const postName = props.postName // the variable you want to filter for
const className = props.className;
const children = props.children;
const { data: { allFile: { edges } } } = props;
const oneImage = edges.filter(edge =>
edge.node.childImageSharp.fluid.originalName === postName)[0].node.childImageSharp.fluid;
Source: stackoverflow.com
Related Query
- How to pass a value to a StaticQuery using props string?
- why we cannot pass boolean value as props in React , it always demands string to be passed in my code
- How to pass props without value to component
- How to pass props to a styled component in emotion? Using TypeScript
- How to do POST in FORM Submit using reactjs and pass the object value into REST service?
- how to pass props to the route when using react router <Link>
- How to use setFieldValue and pass the value as props between components
- How to pass props using element in react-router v6?
- Next.js using getServerSideProps how do i pass props from page to components?
- React Hook Forms How to pass the errors as a props using Typescript
- How to set default value of props if not passed to functional component in NextJS using Typescript
- How to pass props of one child component to another child component using parent component
- How to pass props with react-router using onClick methods
- How to pass Props() value into setState() to make modal form editable using ReactJs
- How to pass props to styled component when using theme?
- How to pass props to route components when using useRoutes?
- How do I pass a value into an GraphQL query using gatsby and react?
- How to pass props using react router link v6?
- How to pass custom props to Material-UI component using makeStyles/withStyles?
- How do I pass props and other parameters to function using React Hooks?
- How do I pass mapped props to a component using useState?
- How to pass value from react to lit-element props
- How to pass state value to a callback function using React Hooks
- How to save data as int using mongodb compass and koa js. Value automatically convert into String
- How to pass the props value from the other sibling components in React JS
- How to pass props from container component to redux form(component) - react using typescript
- How to pass props if it already has some value passed?
- How to pass props to child components when using protected routes?
- How to pass props to react component through routing using react-router?
- How to test the value of the props of react component rendered using react testing library
More Query from same tag
- How to parse a string with variables into a react component?
- how to configure styled jsx in Eslint
- Pages are reloaded instead of routed in shopify next js app
- Access variable outside of a callback ReactJS
- How do I not allow submission for empty fields from a child component? More explanation below
- Converting inline function to normal function
- Item not being removed from an array
- react calling a function in a child component
- Redux toolkit, dispatching thunk type missing
- Problem using forms and do fetch request in reactjs
- update charjs dataset using react library
- Reuse mutations without duplicating code in Apollo + React?
- How make my cross to be completely fitted by the surrounding container?
- Pass dynamic value into react-i18next useTranslate
- Handling unauthorized request in react-query
- React, React Redux, Redux Form type is invalid
- Recharts - Tooltip Formatter on Biaxial Line Charts?
- HTMLInputElement event.target.value outputting a string event though it is a range element (typescript)
- How to specify typescript type of a component with properties
- Is it possible to use the Symfony form component with React.js?
- Making the state of a component affect the rendering of a sibling when components are rendered iteratively
- CSS: apply :focus style to unfocused element?
- Convert text of a button from 'Copy' to 'Copied' and then back to 'Copy' after few seconds
- ReactNative/Flex: how to arrange two components horizontal?
- Webpack is taking 30 seconds to compile
- Issue in `npm start` command in react.js
- Typescript how to type custom key in an object
- react with GraphQL Clients
- react - conditional rendering inside component return function
- http-only cookie + token : double job?