2

HELP! I'm doing React + Laravel 6. I want to pass a string to my props in my React component but this has to be passed/written from a file.blade.php file.

** file.blade.php **

<div id="MyComponent"></div>

<div id="MyComponent"> string I want to pass</div> I can't get this innerHTML as well as ID is already tagged.

<div id="MyComponent" customProp="string to I want to pass"></div> I'm not sure if it is possible to use a custom property or the native property of an HTML tag and fetch it inside the JS component but I know this is possible for React components.

I know that php files can't type <MyComponent /> inside a blade.php file like I can in JS and php files can only use a component through ID tag. Please don't suggest fetch, i know that it would solve this problem but the process is needlessly longer for the desired effect, just for display.I'd rather hard code this component in HTML.

Desired effect: I want this React Component to adjust it's text based on the passed string. I can simply hard code the text inside JS but component wouldn't be reusable and defeat the componentization.

3 Answers 3

2

You can define your element first as a const/var or whatever. Then you access the element's attributes with element.attributes and iterate through your attributes like so:

const element = document.getElementById('MyComponent');

ReactDOM.render(
    <MyComponent {...element.attributes} />,
    element
);

You can access the attributes with this.props.

If you're passing data-* attributes your can pass those with {...element.dataset}

Sign up to request clarification or add additional context in comments.

1 Comment

the dataset is gold
0

you can use <MyComponent />


<div id="app">
     <MyComponent customProp="string to I want to pass" />
</div>

in app.js

 ReactDOM.render(<App />, document.getElementById('app'));

2 Comments

But I need to type in the attribute in HTML like ``` <div id="MyComponent" customProp="My Message"> </div> ``` in a php file. Not in .js file so the <MyComponent /> won't work inside .php file.
yes u can do that once app components Mount inside id="app" then u can include other components inside that
0

I am not a hardcore reactjs dev but what I did recently for a tool I built with a reactjs dev was pass in my variable as a data attribute like so:

<div id="root" data-text="blah blah">

And then, looking at his source code, I can see a function:

componentDidMount()
{
      //other variables here 
      let data_text= $('#root').attr("data-text"); //get the data attribute variable
}

Hope this helps

3 Comments

Holy! It worked. The .attr is new to me. I haven't really explored jQuery. I wish I've read this yesterday.
Glad it helped @shupesmerga 🤓
This would work, but you already access the element when doing ReactDOM.render(etc.); You can pass the attributes within the ReactDOM.render() function immediately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.