How to access parent block’s attributes in nested block’s save function?

Question

I currently have a block that passes some of its attributes down to the nested InnerBlocks using React’s Context API (the version of WordPress that I’m on does not yet have the Block Context feature. This allows me to successfully access the parent block’s attributes in the nested blocks’ edit function.

registerBlockType("myblock", {
  title: "My Block",
  attributes: {
    someValue: {
      type: 'string',
      default: '',
    },
  },
  edit(props) {
    const { someValue } = props.attributes;

    // ...other code...

    return (
      <MyContext.Provider value={someValue}>
        <InnerBlocks />
      </MyContext.Provider>
    );
  },
  save(props) {
    const { someValue } = props.attributes;

    return (
      <InnerBlocks.Content />
    );
  }
});

However, I also want to access the parent block’s attributes in the save function of the nested blocks. I have not been able to get React’s Context API to work. Is there a method for doing this in WordPress’s Block API?

0
Michael Cheng 3 years 2020-07-09T00:10:37-05:00 0 Answers 42 views 0

Leave an answer

Browse
Browse