Home Passing the value of a variable(string) from a razor view to a React component
Reply: 1

Passing the value of a variable(string) from a razor view to a React component

Negin
1#
Negin Published in 2018-01-11 07:24:55Z

I am wondering if you have any suggestions in regard to passing the string from a razor view to a React component?

export default class WidgetModel extends Component {

  constructor() {
    super()
  }

  render() {
    const {  } = this.props,   
    return (  
      <p>{Copy}</p>           
    )
  }
}
   <div id="widget">
      <p>@vm.Copy</p>
   </div>

Milad
2#
Milad Reply to 2018-01-11 10:26:11Z

As far as I understand, your c# code gets evaluated in the server and at that time, react doesn't exist, and then the code will be passed to the browser and then react will render it.

So I basically you don't have access to your normal variables like you want, but It might be possible to assign your c# value to a global javascript variable so you can pick it up later in the browser :

Say this is your c# code :

<div id="widget">
      <p>@vm.Copy</p>
   </div>

add something like this :

<div id="widget">
      <p>@vm.Copy</p>

      <script>
        var window.Copy = "@vm.Copy"; // we need the quotation so javascript doesn't compile it as a refrence, rather as a string
        console.log('Copy',Copy);
      </script>
   </div>

and then in your react code your should be able to pick it up :

 render() {
    const {  } = this.props,   
    return (  
      <p>{window.Copy}</p>           
    )
  }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.32919 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO