Home > Back-end >  String format /n doesn't work React Native / webview
String format /n doesn't work React Native / webview

Time:06-23

I use webview to add an editor to my app. The content is saved within the variable Content:

    <script>
        function update() {
            var idoc = document.getElementById('iframe').contentWindow.document;
            idoc.open();
            idoc.write(editor.getValue());
            idoc.close();
        }

let Content= "";
...

I push default text to the editor/Content variable via injectedJavaScript:

<WebView
          ref={(r) => (webref = r)}
          androidHardwareAccelerationDisabled={true}
          scalesPageToFit={false}
          scrollEnabled={false}
          source={{ html }}
          injectedJavaScript={props.defaultContent}
            onMessage={(event) => {
            console.log("EVENT-DATA:", event);
            Content(event.nativeEvent.data);
          }}
        />

injectedJavaScript gets the props defaultEditorContent, which looks like this:

      let defaultEditorContent = `Content  = 
      "I am a super long string and I love it!" 
      editor.setValue(Content , 1);
      `;

Everything above works fine. However, I can not format the string "I am a super long string and I love it!".

If I manually add format it like this - no text is shown whatsoever:

 "I am a super long string 
and I love it!" 

If I use \n \n\ there is also no text shown:

 "I am a super long string /n/
and I love it!" 

Any ideas?

CodePudding user response:

This will help to your answer,

let defaultEditorContent = `
  Content = \`<h1 id ="ali">Hello World! \n 
  </h1>\`;
      editor.setValue(Content , 1);
      `;
  • Related